Loading configurable parameters from properties files into Spring bean context definitions using PropertyPlaceholderConfigurer – An example
Using Spring‘s PropertyPlaceholderConfigurer to load configurable parameters in properties files – An example
Here’s a context definition for a bean in XML:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost/db" /> <property name="username" value="user" /> <property name="password" value="pwd" /> </bean>
JDBC properties, though made configurable in the above XML context definition, it’d be better if we dynamically get these values from a properties file kept centrally and shared by all the applications. Use of properties files also makes it easy for Operations/Deployment team to modify the configurable parameters. Spring’s PropertyPlaceholderConfigurer helps you achieve that! It pulls values from a properties file into bean definitions.
By definition, PropertyPlaceholderConfigurer is a property resource configurer that resolves placeholders in bean property values of context definitions.
Let’s see how this can be done:
1. Modify context definition
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
2. Create a new properties file – jdbc.properties with these properties – and add to classpath
# database connection properties jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost/db jdbc.username=user jdbc.password=pwd
3. Pull all the referred jdbc properties from jdbc.properties file
It can be done in two ways:
Method #1: Adding a PropertyPlaceholderConfigurer bean in application context
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc.properties</value> </property> </bean>
Method #2: Using context namespace
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> . . . <context:property-placeholder location="jdbc.properties" /> . . . </beans>
About this entry
You’re currently reading “Loading configurable parameters from properties files into Spring bean context definitions using PropertyPlaceholderConfigurer – An example,” an entry on Singaram's Tech Musings
- Published:
- August 9, 2011 / 7:30 PM
- Category:
- Coding tips, Java Frameworks, Spring
- Tags:
- Java Properties, Spring Framework

5 Comments
Jump to comment form | comment rss [?] | trackback uri [?]