factory-bean in Spring XML Configuration
March 09, 2023
On this page, we will learn to configure factory-bean in Spring XML file. The factory-bean
is a property of <bean>
element. It works with factory-method
property of <bean>
element. To understand factory-bean
, we need to create a factory class which will have non-static methods to return the bean instance.
In our example, we have two service classes and one factory class. Factory class will return the instance of service class.
Find the steps to work with factory-bean.
1. Create a factory class and use
factory-method
attributes to declare factory instance method.
2. Now use
factory-bean
and factory-method
attribute of bean
tag.
3. The
factory-bean
attribute will assign factory bean id and factory-method
will assign factory method.
Find the XML code.
<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>
Complete Example
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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.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>
package 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; } }
package com.concretepage; public class UserService { public UserService() { System.out.println("Initialising UserService."); } }
package com.concretepage; public class LoginService { public LoginService(){ System.out.println("Initialising LoginService."); } }
package 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.