If you can treat blank values as missing, and can use SpEL and the Elvis operator
@Value("#{'${some.value:}' ?: null}")
private String someValue;
This works because a missing some.value
will lead to an empty string which is "falsy" and then the elvis operator goes with the fallback. In the SpEL expression - so in the #{...}
scope - null
means null
and not the string "null".
As compared to @EpicPandaForce's answer:
This does not require a custom PropertySourcesPlaceholderConfigurer
but requires a SpEL expression
and is not global - so each property like that will need the adjustment
Blank values are treated as "missing" too - which may be a pro or a con