Thursday 16 February 2017

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

No comments:

Post a Comment