spring xml configuration

Instantiate Spring Beans using Static Factory Method Example

This tutorial shows complete example on how to instantiate beans in spring framework with a Static Factory Method.

Instantiate beans with a Static Factory Method

In the previous tutorial you have seen how to Instantiate Spring Beans with a Constructor. To instantiate beans using Static Factory Method, you need to use the class attribute to mention the class that contains the static factory method and an attribute named factory-method to mention the factory method name as shown below. And you can set object instance properties or supply constructor arguments to the factory method as shown below.

beans-initialize-staticfactorymethod-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">

	<bean id="department" class="com.sneppets.initializebeans.staticfactorymethod.Department">
	
		<property name="name" value="Sales" />
		
	</bean>
		
	<bean id="employee" class="com.sneppets.initializebeans.staticfactorymethod.Employee" 
			factory-method="getEmployeeInstance">
			<constructor-arg index="0" value="John"/>
			<constructor-arg index="1" ref="department"/>
	</bean>	

</beans>

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

Employee.java

package com.sneppets.initializebeans.staticfactorymethod;

public class Employee {
	
	private String name;
	
	private Department department;
	
	public Employee(String name, Department department) {
		this.name = name;
		this.department = department;
	}
	
	public static Employee getEmployeeInstance(String name, Department department) {
		
		return new Employee(name, department);
	}

	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.staticfactorymethod;

public class Department {

	private String name;

	public String getName() {
		return name;
	}

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

StaticFactoryMethodInstantiationExample.java

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

package com.sneppets.initializebeans.staticfactorymethod;

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

public class StaticFactoryMethodInstantiationExample {

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

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

Output

Employee Name: John
Department Name: Sales

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments