Set default property value to NULL in Spring

I want to determine the default property value in a Spring XML configuration file. I want this default to be null .

Something like that:

 ... <ctx:property-placeholder location="file://${configuration.location}" ignore-unresolvable="true" order="2" properties-ref="defaultConfiguration"/> <util:properties id="defaultConfiguration"> <prop key="email.username" > <null /> </prop> <prop key="email.password"> <null /> </prop> </util:properties> ... 

This does not work. Is it even possible to define default null values ​​for properties in a Spring XML configuration?

+23
spring null properties default-value
May 24 '13 at 12:36
source share
4 answers

Better use Spring EL this way.

 <property name="password" value="${email.password:#{null}}"/> 

it checks if email.password is email.password , and sets it to null (not "null" String) otherwise

+66
Jul 04 '13 at 12:47 on
source share

You can try using Spring EL.

 <prop key="email.username">#{null}</prop> 
+2
May 24 '13 at 12:48
source share

look at PropertyPlaceholderConfigurer # setNullValue (String)

It states that:

By default, this value is not set to zero. This means that it is not possible to express null as a property value unless you explicitly specify an appropriate value.

So, just define the string "null" to display the null value in the PropertyPlaceholderConfigurer:

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="nullValue" value="null"/> <property name="location" value="testing.properties"/> </bean> 

Now you can use it in your property files:

 db.connectionCustomizerClass=null db.idleConnectionTestPeriod=21600 
+2
Apr 03 '14 at 7:14
source share

It looks like you can do the following:

 @Value("${some.value:null}") private String someValue; 

and

 @Bean public static PropertySourcesPlaceholderConfigurer propertyConfig() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setNullValue("null"); return propertySourcesPlaceholderConfigurer; } 
0
Sep 14 '17 at 13:40
source share



All Articles