Learnitweb

Instantiation of bean by using an instance factory method in Spring

Spring allows instantiation of bean using instance factory method. Using instance factory allows to keep separate the logic to create the bean separate. In this article we’ll discuss the instantiation of bean by using instance factory method in Spring.

Instantiation with an instance factory method invokes the non-static method of the existing bean in the container. To do this do not provide value for class attribute and for the factory-bean attribute, specify the name of the bean that contains the instance method to be invoked to create the object. For the factory-method attribute, provide the name of the factory method used to create the object.

Let us now see this with the help of an example.

Following class contains the factory method which returns the Employee object.

package com.learnitweb.factory;

import com.learnitweb.model.Employee;

public class EmployeeFactory {

	private static Employee employee = new Employee();

	public Employee createEmployee() {
		employee.setEmpId(1);
		employee.setFirstName("John");
		employee.setLastName("Holmes");
		return employee;
	}
}
package com.learnitweb.model;

public class Employee {

	private Integer empId;
	private String firstName;
	private String lastName;

	public Employee() {
		super();
	}

	public Integer getEmpId() {
		return empId;
	}

	public void setEmpId(Integer empId) {
		this.empId = empId;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- the factory bean, which contains a method called createInstance() -->
	<bean id="employeeFactory"
		class="com.learnitweb.factory.EmployeeFactory">
	</bean>

	<!-- the bean to be created via the factory bean -->
	<bean id="emp" factory-bean="employeeFactory"
		factory-method="createEmployee" />
</beans>

Let us now test our example.

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

import com.learnitweb.model.Employee;

public class Demo {
	public static void main(String args[]) {
		ApplicationContext context = new ClassPathXmlApplicationContext("app-context.xml");

		Employee employee = (Employee) context.getBean("emp");
		System.out.println(employee);

	}

}

Output

Employee [empId=1, firstName=John, lastName=Holmes]