Spring Autowire By Name

This tutorial shows how to use spring autowire by name. When the autowire by name is enabled, spring will look for a bean with the same name as the property that needs to be autowired. For example, suppose we have a Person bean that exposes a Job property. Spring will find the Job bean, if one exists and wire it automatically. If no matching bean is found, then no wiring is executed.

Beans to be autowired

We have two beans Person and Job. Not that the Person bean has a property of Job, we are going to autowire this property. Make sure this property has a setter method, otherwise the injection will not work because it is setter based DI.

package com.memorynotfound.spring.core.autowired;

public class Person {

    private String name;
    private Job job;

    public void setName(String name) {
        this.name = name;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", job=" + job +
                '}';
    }
}

This Job bean will be autowired into the Person bean.

package com.memorynotfound.spring.core.autowired;

public class Job {

    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Job{" +
                "name='" + name + '\'' +
                '}';
    }
}

Spring Explicit Wiring

You can wire the bean explicitly using the <property/> element given a name of the property and a ref attribute that references the bean to be wired.

<bean id="person" class="com.memorynotfound.spring.core.autowired.Person">
    <property name="name" value="John Doe"/>
    <property name="job" ref="job"/>
</bean>
<bean id="job" class="com.memorynotfound.spring.core.autowired.Job">
    <property name="name" value="Java Developer"/>
</bean>

Output

This will produce the following output if we call the toString method of the Person bean.

Person{name='John Doe', job=Job{name='Java Developer'}}

Spring Autowire By Name

With spring autowire by name enabled, the wiring is done implicitly. Which means that you no longer need to explicitly wire the beans together by providing the <property/> element of the bean. The wiring is done by name, which means that as long as there is a bean with the same name as the complementary property in that class, the autowiring is done automatically.

<bean id="person" class="com.memorynotfound.spring.core.autowired.Person" autowire="byName">
    <property name="name" value="John Doe"/>
</bean>
<bean id="job" class="com.memorynotfound.spring.core.autowired.Job">
    <property name="name" value="Java Developer"/>
</bean>

Output

This will produce the following output if we call the toString method of the Person bean.

Person{name='John Doe', job=Job{name='Java Developer'}}

Spring Autowire by Name – no candidate

With spring autowire by name enabled and there is no qualifying bean that matches the name, then no autowiring is performed.

<bean id="person" class="com.memorynotfound.spring.core.autowired.Person" autowire="byName">
    <property name="name" value="John Doe"/>
</bean>
<bean id="job-name-not-matched" class="com.memorynotfound.spring.core.autowired.Job">
    <property name="name" value="Java Developer"/>
</bean>

Output

This will produce the following output if we call the toString method of the Person bean.

Person{name='John Doe', job=null}

References

Download

You may also like...