Home » Java » Enterprise Java » Spring Property Placeholder Configurer – A few not so obvious options

About Biju Kunjummen

Biju Kunjummen

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:


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'
  <property name='driverClassName' value='${jdbc.driverClassName}'/>
  <property name='url' value='${jdbc.url}'/>
  <property name='username' value='${jdbc.username}'/>
  <property name='password' value='${jdbc.password}'/>

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;

public BasicDataSource dataSource() {
 BasicDataSource dataSource = new BasicDataSource();
 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;

public BasicDataSource dataSource() {

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

phase.properties file –


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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
4 Comments Views Tweet it!
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 our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
Email address:

Leave a Reply

4 Comments on "Spring Property Placeholder Configurer – A few not so obvious options"

newest oldest most voted
Notify of
Orest Ivasiv

thanks for sharing, especially default and nested props.


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


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


Thanks so much!!!!!! ^^