Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 562
Darshan Mehta Avatar asked Feb 06 '15 15:02

Darshan Mehta


People also ask

How does Spring Boot determine property value?

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.

How do I change the default value in application properties?

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.

How do you get all properties in Spring boot application properties?

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.


2 Answers

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.

like image 160
Ophir Radnitz Avatar answered Oct 07 '22 17:10

Ophir Radnitz


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.

like image 20
Arpit Jain Avatar answered Oct 07 '22 17:10

Arpit Jain