Monday, 20 February 2017

Spring : Specifying the properties using PropertyPlaceholderConfigurer

In my previous posts, I explained how to load one (or) more property files using PropertyPlaceholderConfigurer class. In addition to this, you can also specify the properties directly in the bean definition itself.
 <bean id="dbProperties"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <!-- typed as a java.util.Properties -->
  <property name="properties">
   <value>
    db.hostName=abc.xyz.com
    db.port=8081
    db.userName=krish@gmail.com
    db.password=secret
   </value>
  </property>
 </bean>


Following is the complete working application.


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

  <!-- typed as a java.util.Properties -->
  <property name="properties">
   <value>
    db.hostName=abc.xyz.com
    db.port=8081
    db.userName=krish@gmail.com
    db.password=secret
   </value>
  </property>
 </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]

Previous                                                 Next                                                 Home

No comments:

Post a Comment