Spring Property Placeholder Configurer – A few not so obvious options

Spring’s PropertySourcesPlaceholderConfigurer is used for externalizing properties from the Spring bean definitions defined in XML or using Java Config. There are a few options that PlaceholderConfigurer supports that are not obvious from the documentation but are interesting and could be useful.

To start with, an example from Spring’s documentation, consider a properties file with information to configure a datasource:
 
 
 
 

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

The PropertySourcesPlaceholderConfigurer is configured using a custom namespace:

<context:property-placeholder location='database.properties'/>

A datasource bean making use of these properties can be defined using XML based bean definition this way:

<bean id='dataSource' destroy-method='close'
    class='org.apache.commons.dbcp.BasicDataSource'>
  <property name='driverClassName' value='${jdbc.driverClassName}'/>
  <property name='url' value='${jdbc.url}'/>
  <property name='username' value='${jdbc.username}'/>
  <property name='password' value='${jdbc.password}'/>
</bean>

and using Java based configuration this way:

@Value('${jdbc.driverClassName}') private String driverClassName;
@Value('${jdbc.url}')             private String dbUrl;
@Value('${jdbc.username}')        private String dbUserName;
@Value('${jdbc.password}')        private String dbPassword;

@Bean
public BasicDataSource dataSource() {
 BasicDataSource dataSource = new BasicDataSource();
 dataSource.setDriverClassName(driverClassName);
 dataSource.setUrl(dbUrl);
 dataSource.setUsername(dbUserName);
 dataSource.setPassword(dbPassword);
 return dataSource;
}

The not so obvious options are:

First is the support for default values. Say for eg, if ‘sa’ is to be provided as default for the jdbc user name, the way to do it is this way(using a ${propertyName:default} syntax) :

<property name='username' value='${jdbc.username:sa}'/>

or with Java Config:

..
..
@Value('${jdbc.username:sa}') private String dbUserName;

@Bean
public BasicDataSource dataSource() {
..
}

Second is the support for nested property resolution, for eg consider the following properties:

phase.properties file –

phase=qa
jdbc.username.qa=qasa
jdbc.username.dev=devsa

and using the ‘phase’ property as part of another property in XML bean definition in this nested way:

<property name='username' value='${jdbc.username.${phase}}'/>

These options could be very useful for place holder based configuration.
 

Reference: Spring Property Placeholder Configurer – A few not so obvious options from our JCG partner Biju Kunjummen at the all and sundry blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

3 Responses to "Spring Property Placeholder Configurer – A few not so obvious options"

  1. thanks for sharing, especially default and nested props.

  2. JV says:

    Been looking for this for a long time. Thanks for sharing

  3. ben says:

    Thanks so much – nested properties is exactly what i have been looking for.

Leave a Reply


8 × nine =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close