Spring Download: Spring always assigns a default value to a property, although it is present in the .properties file

I am working with Spring boot 1.1.8, which uses Spring 4.0.7. I automatically create properties in my classes using the @Value annotation. I want to have a default value if the property is not in the properties file, so I use ":" to assign the default value. The following is an example:

@Value("${custom.data.export:false}") private boolean exportData = true; 

It should assign false to the variable if the property is not in the properties file that it does. However, an if property is present in the file, then it assigns a default value and ignores the value of the property. For instance. if I defined a property similar to the one mentioned above and the 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 someone explain to me what I'm doing wrong here?

thanks

+6
source share
2 answers

We were bitten by the following Spring bug with exactly the same symptom:

[SPR-9989] Using Multiple Gaps PropertyPlaceholderConfigurer @ Default is the default

Basically, if there is more than one PropertyPlaceholderConfigurer in the ApplicationContext, only the predefined defaults will be allowed, and there will be no overrides. Setting a different ignoreUnresolvablePlaceholders did not affect the question, and both (true / false) worked equally well in that regard as soon as we removed the extra PropertyPlaceholderConfigurer .

In it, each of the defined PropertyPlaceholderConfigurer internally resolved the properties as expected, but Spring was unable to determine which one to use to enter the value into the / params @Value annotated fields.

+8
source

You can do one of the following to overcome this:

  • Use custom Separator value in your configurator

 <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> 
  1. Increase the preference of the corresponding configurator using the order property

 <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 .

+2
source

Source: https://habr.com/ru/post/982099/


All Articles