Spring Autowiring by Type

In Spring, “Autowiring by Type” means, if data type of a bean is compatible with the data type of other bean property, auto wire it.

For example, a “person” bean exposes a property with data type of “ability” class, Spring will find the bean with same data type of class “ability” and wire it automatically. And if no matching found, just do nothing.

You can enable this feature via autowire="byType" like below :


	<!-- person has a property type of class "ability" -->
	<bean id="person" class="com.mkyong.common.Person" autowire="byType" />
		
	<bean id="invisible" class="com.mkyong.common.Ability" >
		<property name="skill" value="Invisible" />
	</bean>

See a full example of Spring auto wiring by type.

1. Beans

Two beans, person and ability.


package com.mkyong.common;
 
public class Person 
{
	private Ability ability;
	//...
}

package com.mkyong.common;
 
public class Ability 
{
	private String skill;
	//...
}

2. Spring Wiring

Normally, you wire the bean explicitly :


	<bean id="person" class="com.mkyong.common.Person">
		<property name="ability" ref="invisible" />
	</bean>
	
	<bean id="invisible" class="com.mkyong.common.Ability" >
		<property name="skill" value="Invisible" />
	</bean>

Output


Person [ability=Ability [skill=Invisible]]

With autowire by type enabled, you can leave the ability property unset. Spring will find the same data type and wire it automatcailly.


	<bean id="person" class="com.mkyong.common.Person" autowire="byType" />
	
	<bean id="invisible" class="com.mkyong.common.Ability" >
		<property name="skill" value="Invisible" />
	</bean>

Output


Person [ability=Ability [skill=Invisible]]

Wait, what if you have two beans with same data type of class “ability”?


	<bean id="person" class="com.mkyong.common.Person" autowire="byType" />
	
	<bean id="steal" class="com.mkyong.common.Ability" >
		<property name="skill" value="Steal" />
	</bean>
	
	<bean id="invisible" class="com.mkyong.common.Ability" >
		<property name="skill" value="Invisible" />
	</bean>

Output


Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: 
...
No unique bean of type [com.mkyong.common.Ability] is defined: 
expected single matching bean but found 2: [steal, invisible]; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No unique bean of type [com.mkyong.common.Ability] is defined: 
expected single matching bean but found 2: [steal, invisible]

In this case, you will hits the UnsatisfiedDependencyException error message.

Note
In autowiring by type mode, you just have to make sure only one unique data type of bean is declared.

Download Source Code

Download it – Spring-AutoWiring-by-Type-Example.zip (6 KB)

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
6 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
6 Comment authors
manouytironhideSSEddiealonso_50 Recent comment authors
newest oldest most voted
manouyt
Guest
manouyt

nice

alonso_50
Guest
alonso_50

Thanks for this post, really helps me to solve some doubts :-)

lsoares13
Guest
lsoares13

how to solve the last problem stated? thx

alonso_50
Guest
alonso_50
SS
Guest
SS

Use

ironhide
Guest
ironhide

why do you want to do that?

trackback
Spring Autowiring by Constructor

[…] June 9, 2011 , Author: mkyongprintIn Spring, “Autowiring by Constructor” is actually autowiring by Type in constructor argument. It means, if data type of a bean is same as the data type of other bean […]

trackback
Spring Auto-Wiring Beans

[…] class="com.mkyong.common.Person" /> </beans>See full example – Spring Autowiring by Type.4. Auto-Wiring ‘constructor’Auto-wire a bean by property data type in constructor […]

Eddie
Guest
Eddie

Nice article

Here is one more short and easy to understand article:

http://www.techburps.com/spring-framework/autowiring-by-name/54/