How to add default value to Spring Boot @Value annotation?
Spring Boot @Value annotation injects property values from application.properties file into spring bean. Here is
the syntax to inject property value into spring bean:
@Value("${property_name}")
|
An example of above syntax is:
@Value("${io.retry.times}")
private int retryCount;
|
Incase we forget to add this property into application.properties file, spring will throw an exception. To avoid this,
@Value annotation allows us to provide default value, the syntax is as shown below:
@Value("${property_name:default_value}")
|
And the sample code is: In the below case, by default, 5 will be assinged to retryCount variable.
@Value("${io.retry.times:5}")
private int retryCount;
|
Assign NULL value as a default value
@Value("${property_name:#{null}}")
|
|