spring xml configuration

Instantiate Spring Beans with a Constructor Example

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

Instantiate beans with a Constructor

To instantiate spring beans using a constructor, you need to specify the bean class and set properties or constructor arguments after the object is constructed a shown in the following bean declaration.

beans-initialize-constructor-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.constructor.Department">
	
		<property name="name" value="Sales" />
		
	</bean>
		
	<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>
	

</beans>

Department.java

package com.sneppets.initializebeans.constructor;

public class Department {

	private String name;

	public String getName() {
		return name;
	}

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

Employee.java

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

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

}

Note, to set the values for constructor arguments we have used “Constructor argument name” way as explained in the following tutorial. Hence to explicitly name your constructor arguments in your Employee class, we have used @ConstructorProperties in the above code.

ConstructorInstantiationExample.java

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

package com.sneppets.initializebeans.constructor;

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

public class ConstructorInstantiationExample {

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

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

When you try to run above test class you would get the following output, where the employee bean is instantiated by Spring container using constructor approach by setting values for constructor arguments <constructor-arg/>.

Output

Employee Name: John
Department Name: Sales

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments