What is the correct way to specify that @Value is not required?
Working on the assumption that by ‘not required’ you mean null
then…
You have correctly noted that you can supply a default value to the right of a :
character. Your example was @Value("${myValue:DEFAULT}")
.
You are not limited to plain strings as default values. You can use SPEL expressions, and a simple SPEL expression to return null
is:
@Value("${myValue:#{null}}")