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

Spring: pass arguments to instance factory method

This is continuation to my previous post. In my previous post, I explained how to instantiate bean using instance factory method. But in real cases, previous example is not much useful. In a non-trivial application, instance factory method takes one (or) more arguments, based on the arguments, it return an object.

By using <constructor-arg> element, you can pass arguments to instance factory method.

For example,
public Shape getShape(String shapeType) {

 if ("square".equals(shapeType)) {
  return new Square();
 } else if ("rectangle".equals(shapeType)) {
  return new Rectangle();
 } else if ("sircle".equals(shapeType)) {
  return new Circle();
 } else {
  return new Shape();
 }

}

Following statements pass argument ‘circle’ to instance factory method getShape.
<bean id="circleBean" name="circleBean" factory-bean="shapeFactoryBean" factory-method="getShape">
 <constructor-arg name="shapeType" value="circle"></constructor-arg>
</bean>

<bean id="shapeFactoryBean" class="com.sample.pojo.ShapeFactory" />

Following is the complete working application.


Shape.java
package com.sample.pojo;

public class Shape {

 public void draw() {
  System.out.println("Drawing shape");
 }
}

Square.java
package com.sample.pojo;

public class Square extends Shape {
 public void draw() {
  System.out.println("Drawing Square");
 }
}

Rectangle.java
package com.sample.pojo;

public class Rectangle extends Shape {
 public void draw() {
  System.out.println("Drawing Rectangle");
 }
}

Circle.java
package com.sample.pojo;

public class Circle extends Shape {
 public void draw() {
  System.out.println("Drawing Circle");
 }
}

ShapeFactory.java
package com.sample.pojo;

public class ShapeFactory {

 public Shape getShape(String shapeType) {

  if ("square".equals(shapeType)) {
   return new Square();
  } else if ("rectangle".equals(shapeType)) {
   return new Rectangle();
  } else if ("circle".equals(shapeType)) {
   return new Circle();
  } else {
   return new Shape();
  }

 }

}

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="squareBean" name="squareBean" factory-bean="shapeFactoryBean"
  factory-method="getShape">
  <constructor-arg name="shapeType" value="square"></constructor-arg>
 </bean>

 <bean id="rectangleBean" name="rectangleBean" factory-bean="shapeFactoryBean"
  factory-method="getShape">
  <constructor-arg name="shapeType" value="rectangle"></constructor-arg>
 </bean>


 <bean id="circleBean" name="circleBean" factory-bean="shapeFactoryBean"
  factory-method="getShape">
  <constructor-arg name="shapeType" value="circle"></constructor-arg>
 </bean>

 <bean id="shapeFactoryBean" class="com.sample.pojo.ShapeFactory" />

</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Shape;

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

  Shape square = context.getBean("squareBean", Shape.class);
  Shape rectangle = context.getBean("rectangleBean", Shape.class);
  Shape circle = context.getBean("circleBean", Shape.class);

  square.draw();
  rectangle.draw();
  circle.draw();

  ((ClassPathXmlApplicationContext) context).close();
 }
}

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

Drawing Square
Drawing Rectangle
Drawing Circle



Previous                                                 Next                                                 Home

Tuesday, 14 February 2017

Spring: Instantiate bean using an instance factory method

In my previous post, I explained how to define a bean using static factory method. In this post, I am going to explain how to instantiate bean using instance factory method.

First step is, you need to define a factory bean.

<bean id="shapeFactoryBean" class="com.sample.pojo.ShapeFactory" />

By specifying the factory-bean and factory-method, you can instantiate a bean.

<bean id="shape" name="shape" factory-bean="shapeFactoryBean" factory-method="getShape" />

Following is the complete working application.

Shape.java
package com.sample.pojo;

public class Shape {

 public void draw(){
  System.out.println("Drawing shape");
 }
}

ShapFactory.java
package com.sample.pojo;

public class ShapeFactory {

 public Shape getShape(){
  return new Shape();
 }
}


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="shapeFactoryBean" class="com.sample.pojo.ShapeFactory" />

 <bean id="shape" name="shape" factory-bean="shapeFactoryBean"
  factory-method="getShape" />

</beans>

HelloWorld.java
package com.sample.test;

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

import com.sample.pojo.Shape;

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

  Shape shape = (Shape) context.getBean("shape");
  
  shape.draw();

  ((ClassPathXmlApplicationContext) context).close();
 }
}

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


Drawing shape







Previous                                                 Next                                                 Home

Sunday, 12 February 2017

Spring: pass arguments to static factory method

This is continuation to my previous post. In my previous post, I explained how to instantiate bean using static factory method. But in real cases, previous example is not much useful. In a non-trivial application, static factory method takes one (or) more arguments, based on the arguments, it return an object.

By using <constructor-arg> element, you can pass arguments to static factory method.

For Ex:
public static Shape getShape(String shapeType) {

 if ("square".equals(shapeType)) {
  return new Square();
 } else if ("rectangle".equals(shapeType)) {
  return new Rectangle();
 } else if ("circle".equals(shapeType)) {
  return new Circle();
 } else {
  return new Shape();
 }

}

Following statement pass the argument value ‘square’ to getShape method.

 <bean id="squareBean" name="squareBean" class="com.sample.pojo.ShapeFactory"
  factory-method="getShape">
  <constructor-arg name="shapeType" value="square"></constructor-arg>
 </bean>

Following is the complete working application.


Shape.java
package com.sample.pojo;

public class Shape {

 public void draw() {
  System.out.println("Drawing shape");
 }
}


Square.java
package com.sample.pojo;

public class Square extends Shape {
 public void draw() {
  System.out.println("Drawing Square");
 }
}

Rectangle.java
package com.sample.pojo;

public class Rectangle extends Shape {
 public void draw() {
  System.out.println("Drawing Rectangle");
 }
}

Circle.java
package com.sample.pojo;

public class Circle extends Shape {
 public void draw() {
  System.out.println("Drawing Circle");
 }
}

ShapeFactory.java
package com.sample.pojo;

public class ShapeFactory {

 public static Shape getShape(String shapeType) {

  if ("square".equals(shapeType)) {
   return new Square();
  } else if ("rectangle".equals(shapeType)) {
   return new Rectangle();
  } else if ("sircle".equals(shapeType)) {
   return new Circle();
  } else {
   return new Shape();
  }

 }

}

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="squareBean" name="squareBean" class="com.sample.pojo.ShapeFactory"
  factory-method="getShape">
  <constructor-arg name="shapeType" value="square"></constructor-arg>
 </bean>

 <bean id="rectangleBean" name="rectangleBean" class="com.sample.pojo.ShapeFactory"
  factory-method="getShape">
  <constructor-arg name="shapeType" value="rectangle"></constructor-arg>
 </bean>


 <bean id="circleBean" name="circleBean" class="com.sample.pojo.ShapeFactory"
  factory-method="getShape">
  <constructor-arg name="shapeType" value="circle"></constructor-arg>
 </bean>

</beans>

HelloWorld.java

package com.sample.test;

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

import com.sample.pojo.Shape;

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

  Shape square = context.getBean("squareBean", Shape.class);
  Shape rectangle = context.getBean("rectangleBean", Shape.class);
  Shape circle = context.getBean("circleBean", Shape.class);

  square.draw();
  rectangle.draw();
  circle.draw();

  ((ClassPathXmlApplicationContext) context).close();
 }
}

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

Drawing Square
Drawing Rectangle
Drawing Circle




Previous                                                 Next                                                 Home

Wednesday, 8 February 2017

Spring: Instantiating beans with static factory method

 ‘factory-method’ attribute of bean tag is used to define a bean using static factory method. Specify the factory method name as value to the attribute ‘factory-method’.

Syntax
<bean id="beanId" class="className" factory-method="factoryMethodName"/>

Example
<bean id="shape" name="shape" class="com.sample.pojo.ShapeFactory" factory-method="getShape"/>

Following is the complete working application.

Shape.java
package com.sample.pojo;

public class Shape {

 public void draw(){
  System.out.println("Drawing shape");
 }
}

ShapeFactory.java
package com.sample.pojo;

public class ShapeFactory {

 public static Shape getShape(){
  return new Shape();
 }
}

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="shape" name="shape" class="com.sample.pojo.ShapeFactory"
  factory-method="getShape" />

</beans>

HelloWorld.java

package com.sample.test;

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

import com.sample.pojo.Shape;

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

  Shape shape = (Shape) context.getBean("shape");
  
  shape.draw();

  ((ClassPathXmlApplicationContext) context).close();
 }
}

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

Drawing shape


You can also pass arguments to static factory method, my next posts explain this.







Previous                                                 Next                                                 Home