Gordon Dickens is an instructor, mentor & consultant. Gordon is currently architecting and teaching several official SpringSource courses and actively tweets about open source technology at http://twitter.com/gdickens. Gordon is active within the Spring Framework community focussed on: Spring training, Spring Roo, Spring Integration, Spring Batch and Eclipse Virgo OSGi projects. Gordon is a DZone MVB and is not an employee of DZone and has posted 39 posts at DZone. You can read more from them at their website. View Full User Profile

Using Default Values for Properties in Spring

12.30.2010
| 20444 views |
  • submit to reddit

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/

Published at DZone with permission of Gordon Dickens, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Danny Wong replied on Thu, 2010/12/30 - 4:23am

Great post. Really miss out such useful feature! Thanks!

Aleksandar Vidakovic replied on Thu, 2010/12/30 - 9:12am

Excellent... thanks.

Cristian Vasile... replied on Thu, 2010/12/30 - 1:52pm

I do something different:

    <bean id="my_propertyPlaceholderConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true" />

        <property name="locations">
            <list>
                <value>classpath:my-default.properties</value>
                <value>classpath:my.properties</value>
            </list>
        </property>
    </bean>

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

And here is another way http://thiamteck.blogspot.com/2008/04/spring-propertyplaceholderconfigurer.html

Julio Argüello replied on Wed, 2011/01/05 - 3:20am

I play with PropertyPlaceholderConfigurers priority value. This make hard knowing which is the value currently being applied, so I wrote a simple class described here: Bluebell and PropertyPlaceholderConfigurer

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.