Spring Autowire By Type
This tutorial shows how to use spring autowire by type. When the autowire by type is enabled, spring will autowire a property if exactly one bean of the property type exists in the container. If there are multiple beans of the same type, a NoUniqueBeanDefinitionException
exception will be thrown. If there are no matching beans, nothing happens and the property will not be initialized.
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 Type
With spring autowire by type 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 type, which means that if a matching type is discovered in the container, the autowiring is done implicit.
<bean id="person" class="com.memorynotfound.spring.core.autowired.Person" autowire="byType">
<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 byType – multiple candidates
With spring autowire by type enabled and there are multiple candidates the container throws a NoUniqueBeanDefinitionException
and fails to start.
<bean id="person" class="com.memorynotfound.spring.core.autowired.Person" autowire="byType">
<property name="name" value="John Doe"/>
</bean>
<bean id="job1" class="com.memorynotfound.spring.core.autowired.Job">
<property name="name" value="Java Developer"/>
</bean>
<bean id="job2" class="com.memorynotfound.spring.core.autowired.Job">
<property name="name" value="Java Developer"/>
</bean>
Output
The container will throw an NoUniqueBeanDefinitionException
exception and fails to start.
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.memorynotfound.spring.core.autowired.Job] is defined: expected single matching bean but found 2: job1,job2
Spring Autowire byType – no candidate
With spring autowire by type enabled and there is no qualifying bean that matches the type, then nothing happens and the bean will not be initialized.
<bean id="person" class="com.memorynotfound.spring.core.autowired.Person" autowire="byType">
<property name="name" value="John Doe"/>
</bean>
Output
This will produce the following output if we call the toString
method of the Person
bean.
Person{name='John Doe', job=null}