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
No comments:
Post a Comment