简体   繁体   中英

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

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

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:

  1. Use custom valueSeparator in your configurer

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

As @Ophir Radnitz stated, this is a spring bug that happens when there is more than one PropertyPlaceholderConfigurer present in the ApplicationContext.

As a workaround, you can obtain the desired behavior with something like that:

(...)

@Autowired
private Environment environment;

(...)

private Boolean shouldExportData()
{        
    return environment.getProperty( "custom.data.export", Boolean.class, Boolean.FALSE );
}

Happening this situation, depends on the type of the parameter.

when setting a default value for a String parameter, your sample code as default value ( @Value("${custom.string:test}") ) works fine, for other types (like boolean , in your question), the default value should be written in this way:

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

similarly, for Integers :

@Value("${custom.integer:#{20}}")

good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM