Guide to instantiate beans in Spring with examples
This tutorial explains several ways to instantiate beans in Spring with examples. As you know that, Spring IoC container manages one or more beans and these beans are created using configuration metadata (For example, XML-based) that you provide to the container. And a bean definition in the XML is used to instantiate or create one or more objects.
Instantiate beans in Spring
To instantiate beans, you need to specify the class for which the object is instantiated in the class attribute of <bean/> element as shown below and it is mandatory.
<bean id="department" class="com.sneppets.initializebeans.constructor.Department"/>
The following are the different ways to instantiate beans in Spring.
- With a Constructor
- Using Static Factory Method
- Using Instance Factory Method.
Instantiate beans with Constructor
When you instantiate beans using Constructor approach, specifying the bean class is sufficient as shown below and Spring IoC Container can manage any class you wanted.
<bean id="employee" class="com.sneppets.initializebeans.constructor.Employee"/>
And to set properties or constructor arguments after the object is constructed, you can follow any one of the following ways
1. Constructor argument name
You can use constructor parameter names and set values or references as shown below
<bean id="employee" class="com.sneppets.initializebeans.constructor.Employee"> <!-- Constructor based dependency injection --> <constructor-arg name="name" value="John"/> <constructor-arg name="department" ref="department"/> </bean>
To explicitly name your constructor arguments in your POJO, you can use @ConstructorProperties as shown below.
package com.sneppets.initializebeans.constructor; import java.beans.ConstructorProperties; public class Employee { private String name; private Department department; //instantiation with a constructor //spring container can inject a department @ConstructorProperties({"name", "department"}) public Employee(String name, Department department) { this.name = name; this.department = department; } //getters and setters }
2. Constructor argument index
Also, you can use the index attribute to specify the index of constructor arguments explicitly as shown below.
<bean id="employee" class="com.sneppets.initializebeans.constructor.Employee"> <constructor-arg index="0" value="John"/> <constructor-arg index="1" ref="department"/> </bean>
3. Constructor argument type matching
You can also use type matching by mentioning the specific type explicitly as shown below.
<bean id="employee" class="com.sneppets.initializebeans.constructor.Employee"> <constructor-arg type="String" value="John"/> <constructor-arg type="com.sneppets.initializebeans.constructor.Department" ref="department"/> </bean>
Instantiate beans with Static Factory Method
To instantiate a bean with static factory method approach, you need to use the class attribute to specify the class that contains the static factory method and an attribute named factory-method to specify the name of the factory method itself as shown below.
<bean id="employee" class="com.sneppets.initializebeans.staticfactorymethod.Employee" factory-method="getEmployeeInstance"/>
The following is an example Employee class which would work with the above bean definition in the XML.
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); } //getters and setters }
The following is the way to set properties or constructor arguments of factory method after the object is returned from the factory.
<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>
Instantiate beans using Instance Factory Method
Similar to instantiating beans with static factory method approach, instantiation of beans using instance factory method invokes a non-static method of a factory bean from container. Therefore in this approach you need to create a factory bean with static factory method and instance (non-static) methods which will be used to create objects as shown below.
package com.sneppets.initializebeans.instancefactorymethod; public class EmployeeFactory { private EmployeeFactory() { } public static EmployeeFactory createEmployeeFactory() { return new EmployeeFactory(); } //instance (non-static) methods 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; } }
Note, our EmployeeFactory class contains private constructor and a static factory method called createEmployeeFactory() which returns EmployeeFactory object.
And EmployeeFactory class contains two non-static methods called getSalesEmployee() and getMarkettingEmployee() used to create objects of type Employee.
The below example shows how to configure such factory bean in the XML-based configuration metadata.
<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 See:
- Event Driven Microservices: Spring Boot + Kubernetes + Docker + Helm + Google Cloud
- Java 8 : Find union and intersection of two Lists (ArrayLists)
- How to copy contents of a List to another List in Java
- Guide to manipulate Java 8 Stream data sources or objects