Example of factory-bean in Spring
April 07, 2013
On this page, we will provide example of factory-bean in spring. factory-bean is an attribute of bean tag in spring. It works with factory-method attribute of bean tag. To understand factory-bean, we need to create a factory class which will have non- static methods to return the bean instance. Factory class must have a static method to return the instance of class itself. For the example, we have two service classes and one factory class. Factory class will return the instance of service classes. We need to follow below steps to work with factory-bean.
1. Create a bean for factory class and use
factory-method
attributes to declare factory instance method.
2. Now we will use
factory-bean
and factory-method
attribute of bean
tag.
3.
factory-bean
attribute will assign factory bean id and factory-method
will assign method of factory class which will return the object of class which will be considered as spring bean.
XML for factory-bean and factory-method
Find the XML file which will show the use offactory-bean
and factory-method
attribute of bean
tag.
spring-config.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <bean id="serviceFactory" class="com.concretepage.ServiceFactory" factory-method="createInstance"> </bean> <bean id="userService" factory-bean="serviceFactory" factory-method="createUserService"> </bean> <bean id="loginService" factory-bean ="serviceFactory" factory-method="createLoginService"> </bean> </beans>
Factory and Service Classes
ServiceFactory.javapackage com.concretepage; public class ServiceFactory { private static UserService userService= new UserService(); private static LoginService loginService= new LoginService(); private static ServiceFactory serviceFactory= new ServiceFactory(); private ServiceFactory(){} public UserService createUserService(){ return userService; } public LoginService createLoginService(){ return loginService; } public static ServiceFactory createInstance(){ return serviceFactory; } }
UserService.java
package com.concretepage; public class UserService { public UserService() { System.out.println("Initialising UserService."); } }
LoginService.java
package com.concretepage; public class LoginService { public LoginService(){ System.out.println("Initialising LoginService."); } }
Main class to run demo
SpringDemo.javapackage com.concretepage; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringDemo { public static void main(String... args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); context.getBean("userService"); context.getBean("loginService"); } }
Initialising UserService. Initialising LoginService.