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 these ads

5 comments

  1. [...] Using Spring’s PropertyPlaceholderConfigurer to load configurable parameters from properties f… (singztechmusings.wordpress.com) [...]

  2. [...] Using Spring’s PropertyPlaceholderConfigurer to load configurable parameters from properties f… (singztechmusings.wordpress.com) [...]

  3. [...] Using Spring’s PropertyPlaceholderConfigurer to load configurable parameters from properties f… (singztechmusings.wordpress.com) [...]

  4. [...] Using Spring’s PropertyPlaceholderConfigurer to load configurable parameters from properties f… (singztechmusings.wordpress.com) [...]

  5. [...] Using Spring’s PropertyPlaceholderConfigurer to load configurable parameters from properties f… (singztechmusings.wordpress.com) [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 26 other followers

%d bloggers like this: