Pages

Saturday, July 31, 2010

Spring – PropertyPlaceholderConfigurer example

Often times, most Spring developers just put the deployment details (database details, log file path) in the bean configuration file as following :


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
<property name="url" value="jdbc:derby://localhost:1527/myeclipse" />
<property name="username" value="classiccars" />
<property name="password" value="classiccars" />
</bean>


In a corporate environment, the deployment detail is usually only can ‘touch’ by your system or database administrator, they just refuse to access your bean configuration file instead of request a new separate file for the deployment configuration.
In Spring, you can use PropertyPlaceholderConfigurer class to externalize the deployment details into a properties file, and access from bean configuration file via a special format – ${variable}.
To run the below example you need below jar files in your class path
  • derbyclient.jar
  • spring-dao.jar
  • spring-jdbc.jar

ApplicationProperties.properties
Create a properties file (ApplicationProperties.properties) and include your deployment details, put it into your project class path.
jdbc.driverClassName=org.apache.derby.jdbc.ClientDriver
jdbc.url=jdbc:derby://localhost:1527/myeclipse
jdbc.username=classiccars
jdbc.password=classiccars
applicationContext.xml
Declare PropertyPlaceholderConfigurer in bean configuration file and map to the ‘ApplicationProperties.properties’ properties file you created just now.
<?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-2.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
<value>ApplicationProperties.properties</value>
</property>
</bean>
<bean id="customerDAO" class="com.SimpleJdbcCustomerDAO">
  <property name="dataSource" ref="dataSource" />
</bean>
<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>
</beans>
SimpleJdbcCustomerDAO.java
package com;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class SimpleJdbcCustomerDAO {
private DriverManagerDataSource dataSource;
/**
* @return the dataSource
*/
public DriverManagerDataSource getDataSource() {
return dataSource;
}
/**
* @param dataSource the dataSource to set
*/
public void setDataSource(DriverManagerDataSource dataSource) {
this.dataSource = dataSource;
}
}

Test.java
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
SimpleJdbcCustomerDAO customerDao = (SimpleJdbcCustomerDAO)context.getBean("customerDAO");
DriverManagerDataSource dataSource = customerDao.getDataSource();
System.out.println("Driver Class Name :::  "+ dataSource.getDriverClassName());
System.out.println("Url :::  "+ dataSource.getUrl());
System.out.println("User Name :::  "+ dataSource.getUsername());
System.out.println("Password :::  "+ dataSource.getPassword());
}
}

Out Put

Driver Class Name :::  org.apache.derby.jdbc.ClientDriver
Url :::  jdbc:derby://localhost:1527/myeclipse
User Name :::  classiccars
Password :::  classiccars

No comments:

Post a Comment