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:

Define Beans in context file
Bean classes
Test the dependency

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 lets see the code of these beans.

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 was injected by type successfully.

Related Articles

post a comment