spring xml configuration

Instantiate Spring Beans using an Instance Factory Method – Example

This tutorial shows complete example on how to instantiate beans in spring framework using an Instance Factory Method.

Instantiate beans using an Instance Factory Method

In the previous tutorials you have seen how to Instantiate Spring Beans with a Constructor and how to Instantiate Spring Beans using Static Factory Method. Instance Factory Method instantiation is similar to static factory method, but it invokes a non-static method of existing bean to create a new bean.

What you need to do is (check bean definitions below the comment <!– beans to be created via factory bean –>), do not specify class attribute, then in the factory-bean attribute mention the name of the factory bean employeeFactory that contains the instance methods getSalesEmployee() and getMarkettingEmployee() that are invoked to create objects. Then set the name of the factory-method with factory-method attribute.

The following is the bean declaration of our example.

beans-initialize-instancefactorymethod-example.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-4.3.xsd">

    <!-- factory bean that contains static factory method called createEmployeeFactory -->
	<bean id="employeeFactory" class="com.sneppets.initializebeans.instancefactorymethod.EmployeeFactory"
			factory-method="createEmployeeFactory"/>
			
	<!-- beans to be created via factory bean -->		
	<bean id="salesEmployee" 
			factory-bean="employeeFactory"
			factory-method="getSalesEmployee">
			<constructor-arg index="0" value="John"/>
	</bean>	
	
	<bean id="markettingEmployee" 
			factory-bean="employeeFactory"
			factory-method="getMarkettingEmployee">
			<constructor-arg index="0" value="Micheal"/>
	</bean>	

</beans>

Also refer to the following classes corresponding to the bean definitions above.

EmployeeFactory.java

package com.sneppets.initializebeans.instancefactorymethod;

public class EmployeeFactory {
	
	private EmployeeFactory() {
		
	}
	
	public static EmployeeFactory createEmployeeFactory() {
		
		return new EmployeeFactory();
	}
	
	public Employee getSalesEmployee(String name) {
		Employee salesEmployee = new Employee();
		salesEmployee.setName(name);
		Department salesDepartment = new Department();
		salesDepartment.setName("Sales");
		salesEmployee.setDepartment(salesDepartment);
		
		return salesEmployee;
	}
	
	public Employee getMarkettingEmployee(String name) {
		Employee markettingEmployee = new Employee();
		markettingEmployee.setName(name);
		Department markettingDepartment = new Department();
		markettingDepartment.setName("Marketting");
		markettingEmployee.setDepartment(markettingDepartment);
		
		return markettingEmployee;
	}

}

Employee.java

package com.sneppets.initializebeans.instancefactorymethod;

public class Employee {
	
	private String name;
	
	private Department department;
	
	public Employee() {
		//default constructor
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

}

Department.java

package com.sneppets.initializebeans.instancefactorymethod;

public class Department {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}

InstanceFactoryMethodInstantiationExample.java

Now, let’s create a test class to perform instantiation of beans with a instance factory method approach.

package com.sneppets.initializebeans.instancefactorymethod;

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

public class InstanceFactoryMethodInstantiationExample {
	
	public static void main (String[] args) {
		
		ApplicationContext context = (ApplicationContext) new ClassPathXmlApplicationContext(
    			"beans-initialize-instancefactorymethod-example.xml");
		
		Employee salesEmp = (Employee) context.getBean("salesEmployee");
		
		System.out.println("Employee Name: " + salesEmp.getName());

		System.out.println("Department Name: " + salesEmp.getDepartment().getName());
		
		Employee markettingEmp = (Employee) context.getBean("markettingEmployee");
		
		System.out.println("Employee Name: " + markettingEmp.getName());

		System.out.println("Department Name: " + markettingEmp.getDepartment().getName());
	}

}

Ouput

Employee Name: John
Department Name: Sales
Employee Name: Micheal
Department Name: Marketting

Also See:

References:

Leave a Reply

avatar
  Subscribe  
Notify of