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 !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “Spring bean autowire byType”

  1. autowire=”byType” is not required in xml.
    @Autowired annotation can be applied on variables and methods for autowiring byType.
    Use @Autowired annotation on constructor for constructor based spring autowiring.

    Reply
  2. Or you can use the @Autowired and remove the get/set method of departmentBean, since the annotation is turned on in the config file.

    Reply
  3. Hi,

    @Autowired
    private DepartmentBean departmentBean;

    I did the above exmple, but i got same output without ‘@Autowired’ annotation.
    Could you pls clarify it?
    Thanks

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.