Spring bean autowire by constructor

In Spring framework, bean autowiring by constructor is similar to byType, but applies to constructor arguments. In autowire enabled bean, it look for class type of constructor arguments, and then do a autowire by type on all constructor arguments.

Please note that if there isn’t exactly one bean of the constructor argument type in the container, a fatal error is raised.

Read More : Spring bean autowiring modes

Bean autowiring by constructor

How to enable constructor autowiring

Autowiring by constructor is enabled by using autowire="constructor" in bean definition in configuration file (i.e. application-context.xml).

A typical bean configuration file will look like this:

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context/
	http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />
    
	<bean id="employee" class="com.howtodoinjava.autowire.constructor.EmployeeBean" autowire="constructor">
		<property name="fullName" value="Lokesh Gupta"/> 
	</bean>
 
	<bean id="department" class="com.howtodoinjava.autowire.constructor.DepartmentBean" >
		<property name="name" value="Human Resource" />
	</bean>

</beans>

Create constructor dependency

In above configuration, I have enabled the autowiring by constructor for ‘employee‘ bean. It has been done by passing constructor arguments.

package com.howtodoinjava.autowire.constructor;

public class EmployeeBean
{
	private String fullName;
	
	public EmployeeBean(DepartmentBean departmentBean)
	{
		this.departmentBean = departmentBean;
	}
	
	private DepartmentBean departmentBean;

	public DepartmentBean getDepartmentBean() {
		return departmentBean;
	}

	public void setDepartmentBean(DepartmentBean departmentBean) {
		this.departmentBean = departmentBean;
	}

	public String getFullName() {
		return fullName;
	}

	public void setFullName(String fullName) {
		this.fullName = fullName;
	}
}

And DepartmentBean looks like this which has been set:

package com.howtodoinjava.autowire.constructor;

public class DepartmentBean{
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Test the dependency

To test that bean has been set properly using constructor based autowiring, run following code:

package com.howtodoinjava.autowire.constructor;

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

public class TestAutowire {
	public static void main(String[] args) {
		ApplicationContext context = 
		    	  new ClassPathXmlApplicationContext(new String[] {"com/howtodoinjava/autowire/constructor/application-context.xml"});
		 
		    	EmployeeBean employee = (EmployeeBean)context.getBean("employee");

		    	System.out.println(employee.getFullName());

		    	System.out.println(employee.getDepartmentBean().getName());
	}
}

Output:

Lokesh Gupta
Human Resource

Clearly, dependency was injected by constructor successfully.

Using constructor-arg

If you are NOT using autowire="constructor" in bean definition, then you will have to pass the constructor-arg as follows to inject department bean in employee bean:

<bean id="employee" class="com.howtodoinjava.autowire.constructor.EmployeeBean">
	<property name="fullName" value="Lokesh Gupta"/> 
	<constructor-arg>
		<ref bean="department" />
	</constructor-arg>
</bean>

Drop me your questions in comments section.

Happy Learning !!

Comments

Subscribe
Notify of
guest

4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.