Spring Bean Autowire by Autodetect

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 ‘autodetect‘ mode.

Autowiring by autodetect uses either of two modes i.e. constructor or byType modes. First it will try to look for valid constructor with arguments, If found the constructor mode is chosen. If there is no constructor defined in bean, or explicit default no-args constructor is present, the autowire byType mode is chosen.

Sections in this post:

Autowire dependency using autodetect
Create no-args constructor in bean
Test the dependency

Autowire dependency using autodetect

Autowiring by autodetect is enabled by using autowire=”autodetect” 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.autodetect.EmployeeBean" autowire="autodetect">

        <property name="fullName" value="Lokesh Gupta"/>

    </bean>

  

    <bean id="department" class="com.howtodoinjava.autowire.autodetect.DepartmentBean" >

        <property name="name" value="Human Resource" />

    </bean>

 

</beans>

Create no-args constructor in bean

In above configuration, I have enabled the autowiring by autodetect for ’employee’ bean. To use the byType mode, I must define either default constructor or no constructor at all.

package com.howtodoinjava.autowire.autodetect;

 

public class EmployeeBean

{

    private String fullName;

     

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

 

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

 

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/autodetect/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 autodetect successfully.

Related Articles

post a comment