Spring Boot : Spring always assigns default value to property despite of it being present in .properties file

Darshan Mehta picture Darshan Mehta · Feb 6, 2015 · Viewed 12k times · Source

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=truethen, 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

Answer

Ophir Radnitz picture Ophir Radnitz · Oct 29, 2015

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.