Spring Bean Autowire byType

Practically bean dependencies are explicitly set in bean configuration files and it is really is a good practice to follow. But Spring is capable of automatically resolving dependencies at runtime. This automatic resolution of bean dependencies is also called autowiring. This type of bean dependencies can also be referred to as collaborating beans or just as collaborators.

There are 5 different types of autowiring modes which are ‘no’, ‘byName’, ‘byType’, ‘constructor’, and ‘autodetect’. In this post, I am taking down ‘byType‘ mode.

Autowiring by type allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean. 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.

Sections in this post:

Enable annotations support
Autowire dependency using @Autowired
Test the dependency

Enable annotations support

We must first enable the annotation configuration support in application because we will be using @Autowired annotation to plug-in the dependency. Annotations are enabled with following tag in bean configuration file.


A typical bean configuration file (e.g. applicationContext.xml) will look like this:

<?xml version="1.0" encoding="UTF-8"?>
    <context:annotation-config />
    
    <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>

Autowire dependency using @Autowired

In above configuration, I have enabled the autowiring by type for ’employee’ bean. It has been done using:

autowire="byType"

Now in EmployeeBean.java, we have to use @Autowired annotation like this:

package com.howtodoinjava.autowire.byType;
import org.springframework.beans.factory.annotation.Autowired;
public class EmployeeBean
{
    @Autowired
    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/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 type successfully.

One thought on “Spring Bean Autowire byType

  1. Pingback: Spring Beans Autowiring Tutorial | Unsekhable

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s