Spring EL
Accessing properties:
$ – value
# – expression/execution
Value
- @Value(“${bean.message}”)
System environment variable
- @Value(“#{systemEnvironment[‘SOME_ENV_VARIABLE’]}”)
It casts to the required type:
@Value(“${daily.limit}”)
int maxTransferPerDay
@Value(“#{environment[‘daily.limit’]}”)
int maxTransferPerDay;
But values are handled as String:
@Value(“#{new Integer(environment[‘daily.limit’]) * 2 }”) -> OK
@Value (“#{new java.net.URI(environment[‘home.page’]).host}”) -> OK
@Value (“#{daily.limit * 2}”}) -> not OK
Default values:
@Value(“${daily.limit:1000}”)
@Value(“#{environment[‘daily.limit’]?:1000}”)
https://www.baeldung.com/spring-expression-language
@Value
This annotation can be placed in:
Constructor
@Autowired
public TransferServiceImpl(@Value("${daily.limit}") int max){
...
}
Method
@Autowired
public int setDailyLimit(@Value("${daily.limit}") int max){
...
}
Value
@Value("${daily.limit}")
int max;