Spring Custom FactoryBean Example

By Arvind Rai, October 22, 2021
This page will walk through Spring custom FactoryBean example. We can create a custom factory bean by implementing Spring FactoryBean interface. For complex initialization code, we can create our own FactoryBean and plug it into the container.
The FactoryBean<T> interface provides three methods.

T getObject() : Returns the instance of the object created by this factory.

boolean isSingleton() : Returns Boolean. Default is true. This method checks whether the object is singleton or not. The instance can be shared or not, depends on whether this factory returns singletons or prototypes.

Class<?> getObjectType() : Returns object type of the object returned by this factory bean. Return type will be null if type is not known in advance.

Create Custom FactoryBean

CustomFactoryBean.java
package com.concretepage;
import org.springframework.beans.factory.FactoryBean;
public class CustomFactoryBean implements FactoryBean<MyBean> {
  private MyBean myBean = new MyBean();

  @Override
  public MyBean getObject() throws Exception {
	System.out.println("inside getObject()");
	return myBean;
  }

  @Override
  public Class<? extends MyBean> getObjectType() {
	System.out.println("inside getObjectType()");
	return myBean.getClass();
  }

  @Override
  public boolean isSingleton() {
	System.out.println("inside isSingleton()");
	return false;
  }
} 
my-app.xml
<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="customfb" class="com.concretepage.CustomFactoryBean"  />
      
</beans>  
MyBean.java
package com.concretepage;
public class MyBean {
  public MyBean() {
	System.out.println("MyBean object is created.");
  }
  public void doTask() {
	System.out.println("Doing my task...");
  }
}
 
SpringAppDemo1.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringAppDemo1 {
  public static void main(String[] args) throws Exception {
	AbstractApplicationContext context = new ClassPathXmlApplicationContext("my-app.xml");
	MyBean myBean = (MyBean) context.getBean("customfb");
	myBean.doTask();
  }
} 
Output
MyBean object is created.
inside isSingleton()
inside isSingleton()
inside getObject()
Doing my task... 

Instantiate FactoryBean with the ampersand symbol (&)

When we need to instantiate FactoryBean itself instead of the bean it produces, prefix the beans’s id with the ampersand symbol (&) when calling the getBean() method of the ApplicationContext.
1. Using ampersand symbol (&) to fetch FactoryBean instance.
FactoryBean<MyBean> factoryBean = (FactoryBean<MyBean>) context.getBean("&customfb"); 
2. Fetching bean created by FactoryBean.
MyBean myBean = (MyBean) context.getBean("customfb"); 

Find the example. SpringAppDemo2.java
package com.concretepage;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringAppDemo2 {
  public static void main(String[] args) throws Exception {
	AbstractApplicationContext context = new ClassPathXmlApplicationContext("my-app.xml");
	FactoryBean factoryBean = (FactoryBean) context.getBean("&customfb");
	MyBean myBean = factoryBean.getObject();
	myBean.doTask();
  }
} 
Output
MyBean object is created.
inside isSingleton()
inside getObject()
Doing my task... 

Reference

Customizing Instantiation Logic with a FactoryBean

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us