Friday, 29 June 2012

Spring Property Place Holder

Basic spring configuration makes using property files and their key-value pairs really easy!

property-placeholder

The most simple way to do this is to use the property-placeholder tag.  This is as simple as including the line,

    <context:property-placeholder location="classpath:app.properties"/>

However, sometimes it is easier to have a list of properties files.  This can simply be done by giving a comma separated list in the example above,

    <context:property-placeholder location="classpath:app.properties,classpath:alt_app.properties"/>

This is starting to get a bit unclear though.  Perhaps a better way is to no longer use the spring property-placeholder and instead use the 'full' bean version.

 PropertyPlaceholderConfigurer

This 'full' bean version is the PropertyPlaceholderConfigurer class and can be used like this,

    <bean id="appProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:app.properties</value>
                <value>classpath:alt_app.properties</value>
            </list>
        </property>
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceholders" value="false" />
    </bean>

This is, to my mind at least, rather more clear.  Spring will mash all the properties file together and present them as a single set of properties.  It will also override values with those found in later files if the keys are the same.  This is a really useful function as you can then specify some items to be overridden but not necessarily all!

We also get to set the ignoreResourceNotFound and ignoreUnresolvablePlaceholders values.

ignoreResourceNotFound:  If a property file cannot be found then spring won't throw an error
ignoreUnresolvablePlaceholders: If a key cannot be found then it is ignored and no error is thrown.

Having ignoreResourceNotFound=true is sensible because you may choose to put in place the ability to override a default properties file but not necessarily populate it.  However, you would normally want all the keys to be defined somewhere so having the ignoreUnresolvablePlaceholders=false is probably going to help diagnose problems.

Referencing a value

A key-value pair can be referenced using the ${key_name} notation.  This can also be used to get properties configured in the jvm with -Dmyprop=value or set into the application server.

Using the PropertyPlaceholderConfigurer you can also set the prefix and suffix characters so that you can specify

        <property name="placeholderPrefix" value="$myapp{" />
        <property name="placeholderSuffix" value="}" />

and then reference your key-value pairs as $myapp{key_name} - or whatever you want.  This is useful where you may have different properties files to control different aspects of your application and you don't necessarily want to leak properties between them.  For example keeping security settings separate.

No comments:

Post a comment