Is there a way to specify the default property value in Spring XML?

We use PropertyPlaceholderConfigurer to use java properties in our Spring configuration ( here)

eg:

<foo name="port"> <value>${my.server.port}</value> </foo> 

We would like to add an additional property, but we have a distributed system in which existing instances can use the default value. Is there a way to avoid updating all of our property files by specifying a default value in spring config if the value of the overriding property is not defined?

+89
java spring xml properties
Mar 25 '10 at 6:26
source share
6 answers

You are looking for a PropertyOverrideConfigurer document registered here.

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-factory-overrideconfigurer

The PropertyOverrideConfigurer property, another bean factory post-processor, is similar to PropertyPlaceholderConfigurer, but unlike the latter, the original definition may have default values โ€‹โ€‹or no values โ€‹โ€‹for the bean properties. If the overriding property file does not have an entry for a specific property, the default context is defined.

+14
Mar 25 '10 at 8:16
source share

Spring 3 supports the syntax ${my.server.port:defaultValue} .

+269
Mar 28 '10 at 23:07
source share

There is a little known feature that makes it even better. You can use a custom default instead of hard-coded, here is an example:

config.properties:

 timeout.default=30 timeout.myBean=60 

context.xml:

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>config.properties</value> </property> </bean> <bean id="myBean" class="Test"> <property name="timeout" value="${timeout.myBean:${timeout.default}}" /> </bean> 

To use the default value, although it can still be easily overridden later, do this in config.properties:

 timeout.myBean = ${timeout.default} 
+27
Jun 26 '14 at 16:40
source share
 <foo name="port"> <value>${my.server.port:8088}</value> </foo> 

should work for you to have 8088 as the default port

See also: http://blog.callistaenterprise.se/2011/11/17/configure-your-spring-web-application/

+24
Oct 02
source share

http://thiamteck.blogspot.com/2008/04/spring-propertyplaceholderconfigurer.html indicates that the "local properties" defined in the bean itself will be considered as default overridden values โ€‹โ€‹read from files:

 <bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"><value>my_config.properties</value></property> <property name="properties"> <props> <prop key="entry.1">123</prop> </props> </property> </bean> 
+8
Aug 30 '10 at 17:42
source share

The default value may be followed : after the property key, for example

 <property name="port" value="${my.server.port:8080}" /> 

Or in Java code:

 @Value("${my.server.port:8080}") private String myServerPort; 

See:

By the way, the Elvis operator is only available in Spring Expression Language (SpEL),
for example: https://stackoverflow.com/a/312960/

+8
May 31 '16 at 7:10
source share



All Articles