Learnitweb

Instantiation of bean with a Static Factory Method in Spring

Spring allows following three ways to instantiate beans:

  • Instantiation with a constructor
  • Instantiation with a static factory method
  • Instantiation by using an instance factory method

In this article we’ll discuss instantiation with a static factory method.

When defining configuration for a bean which is created with a static factory method, we use two attributes class and factory-method. The class attribute specifies the class that contains the static factory method and factory-method specifies the name of the factory method.

Example

We’ll first create the Employee class. This is the class the object of which will be returned by the factory method.

public class Employee {

	private Integer id;
	private String firstName;
	private String lastName;

	private String employeeType;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	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;
	}

	public String getEmployeeType() {
		return employeeType;
	}

	public void setEmployeeType(String employeeType) {
		this.employeeType = employeeType;
	}

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

}

We’ll now create the EmployeeFactory class, which has a factory method createEmployee which returns the Employee object based on the argument.

package com.learnitweb.factory;

public class EmployeeFactory {
	public static Employee createEmployee(String employeeType) {
		if ("manager".equals(employeeType) || "team lead".equals(employeeType)) {
			Employee employee = new Employee();

			employee.setId(-1);
			employee.setFirstName("John");
			employee.setLastName("Holmes");
			employee.setEmployeeType(employeeType);

			return employee;
		} else {
			throw new IllegalArgumentException("Unknown employee type");
		}
	}
}

Let us now define the bean configuration.

<?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">
	<bean id="teamLead" class="com.learnitweb.factory.EmployeeFactory"
		factory-method="createEmployee">
		<constructor-arg value="team lead" />
	</bean>

	<bean id="manager" class="com.learnitweb.factory.EmployeeFactory"
		factory-method="createEmployee">
		<constructor-arg value="manager" />
	</bean>
	
</beans>

Let us now test our code.

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

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

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

		Employee teamLead = (Employee) context.getBean("teamLead");
		System.out.println(teamLead);
	}

}

Output

Employee [id=-1, firstName=John, lastName=Holmes, employeeType=manager]
Employee [id=-1, firstName=John, lastName=Holmes, employeeType=team lead]