Category: java
Spel in spring value and bean configuration
Published on 30 Sep 2026
Explanation
SpEL can be used with @Value to calculate or dynamically resolve values during Spring bean creation.
Code:
@Value("#{10 + 20}")
private int total;
// total = 30
Explanation
SPEL can reference another Spring bean using the @beanName syntax.
Code:
@Value("#{@pricingService.getDiscount()}")
private double discount;
Explanation
SpEL can access properties from application configuration and combine them with expressions.
Code:
@Value("${app.max-users}")
private int maxUsers;
Explanation
Conditional expressions can be used inside @Value to select different values.
Code:
@Value("#{${app.enabled} ? 'ACTIVE' : 'INACTIVE'}")
private String status;
Explanation
SpEL can be used in bean configuration when a bean property needs to be calculated dynamically.
Code:
@Bean
public String applicationMode() {
return "#{systemProperties['app.mode']}";
}