Spring bean autowire byType

In Spring framework, bean autowiring by type allows a property to be autowired –

  1. if there is exactly one bean of the property type in the container.
  2. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean.
  3. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting the dependency-check="objects" attribute value specifies that an error should be thrown in this case.
If you are using @Autowired annotation, then you do not need to provide autowire attribute. By default, @Autowired annotation uses byType autowiring.

Read More : Spring bean autowiring modes

Bean autowiring by type

Define Beans in context file

A typical bean configuration file (e.g. applicationContext.xml) 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">
    
	<bean id="employee" class="com.howtodoinjava.autowire.byType.EmployeeBean" autowire="byType">
		<property name="fullName" value="Lokesh Gupta"/> 
	</bean>
 
	<bean id="department" class="com.howtodoinjava.autowire.byType.DepartmentBean" >
		<property name="name" value="Human Resource" />
	</bean>

</beans>

Bean classes

In above configuration, I have enabled the autowiring by type for ‘employee‘ bean. It has been done using autowire="byType". Now all properties inside employee bean (e.g. departmentBean) will be looked up using byType autowiring.

package com.howtodoinjava.autowire.byType;

import org.springframework.beans.factory.annotation.Autowired;

public class EmployeeBean
{
	private DepartmentBean departmentBean;
	
	private String fullName;

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

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, run following code:

package com.howtodoinjava.autowire.byType;

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/byType/applicationContext.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 of departmentBean was injected by type successfully.

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.