Spring PropertyPlaceholderConfigurer

The PropertyPlaceholderConfigurer is used to externalize property values from a BeanFactory definition, into another separate file in the standard Java Properties format. This is useful to allow the person deploying an application to customize environment-specific properties (for example database URLs, usernames and passwords), without the complexity or risk of modifying the main XML definition file or files for the container.

Following is the XML-based configuration, where a DataSource with placeholder values is defined. We will configure some properties from an external Properties file, and at runtime, we will apply a PropertyPlaceholderConfigurer to the metadata which will replace some properties of the DataSource

	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>db.properties</value>
		</property>
	</bean>

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

Replacing property placeholders

Spring 2.5 made it more easier to configure property placeholder configurer. Now a placeholder configurer can be configured like this:

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

Note : You will need to add in the spring-context-2.5.xsd schema reference

The actual values come from another file in the standard Java Properties format:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=

Following example demonstrates PropertyPlaceholderConfigurer:

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

  <dependencies>
   	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring</artifactId>
		<version>2.5.6</version>
	</dependency>
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.21</version>
    </dependency>
    <dependency>
	 	<groupId>commons-dbcp</groupId>
	 	<artifactId>commons-dbcp</artifactId>
	 	<version>1.4</version>
	</dependency>

  </dependencies>

1. POJO

Create Employee Class as following

package com.kruders.bean;

public class Employee {
	private String name;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

2. Data Access Object

Create EmployeeDao that jdbc to insert a record

package com.kruders.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;

import com.kruders.bean.Employee;

public class EmployeeDao {
	private DataSource dataSource;
	 
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	public void insert(Employee employee){
		 
		String sql = "insert into employee(name) VALUES (?)";
		Connection conn = null;
 		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, employee.getName());
			ps.executeUpdate();
			ps.close();
			conn.close();
		} catch (SQLException sqle) {
			System.out.println("Sql Exception " + sqle.getMessage());
 
		}
	}
}

3. Properties File

Create db.properties that contains your database details

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=

4. Configuration

Create Spring-Bean.xml and write the following code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>db.properties</value>
		</property>
	</bean>

	<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>
	
	<bean id="employee" class="com.kruders.bean.Employee">
	   <property name="name" value="Puneet" />
	</bean>
	
	<bean id="employeeDAO" class="com.kruders.dao.EmployeeDao">
		<property name="dataSource" ref="dataSource" />
	</bean>
</beans>

5. Run Program

Create Main.java class that inserts the employee details and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kruders.bean.Employee;
import com.kruders.dao.EmployeeDao;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Beans.xml");
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getName());
		
		EmployeeDao employeeDAO = (EmployeeDao) context.getBean("employeeDAO");
        employeeDAO.insert(employee);
	}

}

The folder structure of the example is shown below in Figure 48.1

Figure 48.1 Figure 48.1

You can download the source code of this example here.

No comments yet.

Leave a Reply