I am working with Spring boot 1.1.8 which uses Spring 4.0.7. I am autowiring the properties in my classes with @Value annotation. I want to have a default value if the property is not present in properties file so, I use ":" to assign default value. Below is the example:
@Value("${custom.data.export:false}")
private boolean exportData = true;
It should assign false to the variable if property is not present in the properties file which is does. However, if property is present in the file, then also it assigns default value and ignores the properties value.
E.g. if I have defined the property like the one mentioned above and application properties file has something like this custom.data.export=true
then, the value of exportData
will still be false whereas it should be true ideally.
Can anyone please guide me what I am doing wrong here?
Thanks
You can use @Value("${property-name}") from the application. properties if your class is annotated with @Configuration or @Component . You can make use of static method to get the value of the key passed as the parameter.
We can assign default value to a class property using @Value annotation. @Value("Default DBConfiguration") private String defaultName; @Value annotation argument can be a string only, but spring tries to convert it to the specified type.
To see all properties in your Spring Boot application, enable the Actuator endpoint called env . This enables an HTTP endpoint which shows all the properties of your application's environment. You can do this by setting the property management.
We were bitten by the following Spring bug with exactly the same symptom:
[SPR-9989] Using multiple PropertyPlaceholderConfigurer breaks @Value default value behavior
Basically if more than a single PropertyPlaceholderConfigurer
is present in the ApplicationContext, only predefined defaults will be resolved and no overrides will take place. Setting a different ignoreUnresolvablePlaceholders
value had no impact on the matter, and both values (true/false) worked equally well in that regard once we removed the extra PropertyPlaceholderConfigurer
.
Looking into it, each of the defined PropertyPlaceholderConfigurer
internally resolved the properties as expected, but Spring couldn't figure out which of them to use in order to inject a value into the @Value
annotated fields/params.
You can do one of the following to overcome this:
<bean id="customConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${catalina.base}/conf/config2.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="valueSeparator" value="-defVal-"/>
</bean>
<bean id="customConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:${catalina.base}/conf/config2.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="order" value="-2147483648"/>
</bean?
I have done some RnD on this issue, available here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With