Ad

Tuesday, 3 March 2015

Bean creation in spring using factory method

Sometimes we will not need the spring container to instantiate the bean class.Instead we might need to use the Factory method(pattern) to instantiate the bean object.Lets says we have the below hierarchy of Classes
  • IProduct - Interface for  all the Product Classes
  • ProductOne,ProductTwo- Product concrete Implementation classes which implements the IProduct Interface
  • ProductFactory - Factory Class which creates the Object of type IProduct
IProduct.java

package test;

public interface IProduct {
public abstract void  doOperation();
}

ProductOne.java

package test;

public class ProductOne implements IProduct{

public ProductOne(String name) {
super();
this.name = name;
}

String name;

public String getName() {
return name;
}

@Override
public void doOperation() {
System.out.println("Done for Product:"+name);
}
}

ProductTwo.java

package test;

public class ProductTwo implements IProduct{
public ProductTwo(String name) {
super();
this.name = name;
}

String name;

public String getName() {
return name;
}

@Override
public void doOperation() {
System.out.println("Done for Product:"+name);
}
}


ProductFactory.java

package test;

public class ProductFactory {
public static IProduct getProduct(int productId)
{
IProduct product=null;
if(productId==1)
{
product=new ProductOne("ProductOne");
}
else if(productId==2)
{
product=new ProductTwo("ProductTwo");
}
return product;
}

}

we should use the class attribute to specify the name of the factory class and the factory-method attribute to specify the static factory method which will instantiate the bean.We can pass the value to this factory method by using the constructor-arg tag as shown below.

productcontext.xml

<?xml version="1.0" encoding="UTF-8"?&gt;
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="productbean1" class="test.ProductFactory" factory-method="getProduct">
<constructor-arg  value="1"/>
</bean>
<bean id="productbean2" class="test.ProductFactory" factory-method="getProduct">
<constructor-arg  value="2"/>
</bean>
</beans>


ProductDemo.java

package test;

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

public class ProductDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext bf= new ClassPathXmlApplicationContext(new String[]            
               {"productcontext.xml"});
   IProduct product1= bf.getBean("productbean1",IProduct.class);
   product1.doOperation();
   IProduct product2=bf.getBean("productbean2",IProduct.class);
   product2.doOperation();
}
}

When we ask for the bean productbean1 and productbean2,the spring container will delegate the call to the factory class "ProductFactory" to instantiate the bean object

OUTPUT

Done for Product:ProductOne
Done for Product:ProductTwo

No comments:

Post a Comment