Using Default Values for Properties in Spring
I was looking through the Spring Greenhouse application and discovered an existing feature that I was unaware of. We can set a default value when configuring PropertyPlaceholderConfigurer.
1. Set the default value separator in config
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:application.properties" />
<property name="ignoreResourceNotFound" value="true" /> <-- Ignore file not found -->
<property name="ignoreUnresolvablePlaceholders" value="true" /> <-- Ignore when tokens are not found -->
<!-- Token that separates default values on a placeholder-by-placeholder basis e.g. ${server.name?localhost} -->
<property name="valueSeparator" value="?" />
...
</bean>
2. Set the default values for your properties
<bean id="myServer" class="com.gordondickens.myapp.MyServerConfig">
<property name="serverName" value="${server.name?localhost}" />
<property name="serverPort" value="${server.port?25}" />
...
</bean>
Notes
- If unspecified, the default separator is a colon “:”
- This option is not available when using the “context” namespace – (submitted Jira ticket: SPR-7794)
From http://gordondickens.com/wordpress/2010/12/06/using-default-values-for-properties-in-spring/
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Danny Wong replied on Thu, 2010/12/30 - 4:23am
Aleksandar Vidakovic replied on Thu, 2010/12/30 - 9:12am
Cristian Vasile... replied on Thu, 2010/12/30 - 1:52pm
I do something different:
This way, I can keep my defaults in one place instead of having to look for them in all the files.
Aravind Yarram replied on Thu, 2010/12/30 - 4:59pm
Julio Argüello replied on Wed, 2011/01/05 - 3:20am