Thursday 16 February 2017

Spring: PropertyPlaceholderConfigurer: load property file from class path

By using 'PropertyPlaceholderConfigurer' class, you can load the properties. Following post explains one of the use case, where you specify the property file location and use the properties defined in the property file.
<bean id="mailProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location" value="classpath:db.properties" />
</bean>

Above statement tells the spring to load properties from the file ‘db.properties’ (Since I am using the keyword ‘classpath’, db.properties file must exist in classpath) to resolve any placeholders ${…} found.


Following are the entries in db.properties file.
db.hostName=abc.xyz.com
db.port=8081
db.userName=krish@gmail.com
db.password=secret

Following bean declaration uses some placeholders which will be resolved by spring.
 <bean id="database" name="database" class=" com.sample.pojo.Database">
  <property name="hostName" value="${db.hostName}" />
  <property name="port" value="${db.port}" />
  <property name="userName" value="${db.userName}" />
  <property name="password" value="${db.password}" />
 </bean>
Following is the complete working application.


db.properties
db.hostName=abc.xyz.com
db.port=8081
db.userName=krish@gmail.com
db.password=secret


Database.java
package com.sample.pojo;

public class Database {
 private String hostName;
 private String port;
 private String userName;
 private String password;

 public String getHostName() {
  return hostName;
 }

 public void setHostName(String hostName) {
  this.hostName = hostName;
 }

 public String getPort() {
  return port;
 }

 public void setPort(String port) {
  this.port = port;
 }

 public String getUserName() {
  return userName;
 }

 public void setUserName(String userName) {
  this.userName = userName;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Database [hostName=").append(hostName).append(", port=").append(port).append(", userName=")
    .append(userName).append(", password=").append(password).append("]");
  return builder.toString();
 }

}

myConfiguration.xml
<?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 id="dbProperties"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="location" value="classpath:db.properties" />

 </bean>

 <bean id="database" name="database" class=" com.sample.pojo.Database">
  <property name="hostName" value="${db.hostName}" />
  <property name="port" value="${db.port}" />
  <property name="userName" value="${db.userName}" />
  <property name="password" value="${db.password}" />
 </bean>

</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Database;

public class HelloWorld {
 public static void main(String args[]) {
  ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

  Database database = context.getBean("database", Database.class);
  System.out.println(database);
  
  ((ClassPathXmlApplicationContext) context).close();
 }
}


Run HelloWorld.java, you can able to see following output.

Database [hostName=abc.xyz.com, port=8081, userName=krish@gmail.com, password=secret]

Note
a. Load the property file from WEB-INF folder
<property name="location" value="WEB-INF/db.properties" />

b. Load the property file from external location
<property name="location" value="file:///C:/Users/db.properties" />






Previous                                                 Next                                                 Home

No comments:

Post a Comment