Spring Autowiring @Qualifier example

In Spring, @Qualifier means, which bean is qualify to autowired on a field. See following scenario :

Autowiring Example

See below example, it will autowired a “person” bean into customer’s person property.

package com.mkyong.common;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
 
public class Customer {
 
	@Autowired
	private Person person;
	//...
}

But, two similar beans “com.mkyong.common.Person” are declared in bean configuration file. Will Spring know which person bean should autowired?

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
<bean 
class ="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
 
	<bean id="customer" class="com.mkyong.common.Customer" />
 
	<bean id="personA" class="com.mkyong.common.Person" >
		<property name="name" value="mkyongA" />
	</bean>
 
	<bean id="personB" class="com.mkyong.common.Person" >
		<property name="name" value="mkyongB" />
	</bean>
 
</beans>

When you run above example, it hits below exception :

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
	No unique bean of type [com.mkyong.common.Person] is defined: 
		expected single matching bean but found 2: [personA, personB]

@Qualifier Example

To fix above problem, you need @Quanlifier to tell Spring about which bean should autowired.

package com.mkyong.common;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
 
public class Customer {
 
	@Autowired
	@Qualifier("personA")
	private Person person;
	//...
}

In this case, bean “personA” is autowired.

Customer [person=Person [name=mkyongA]]

Download Source Code

Reference

  1. Spring @Autowired example
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • http://www.storenvy.com/jeroldltpfpqh which diet pills work the best

    At Airlangga University a double-blind investigation was carried out, and Tribulus ended up being demonstrated to substantially boost DHEA concentrations within the body of mature men voyager v3 diet pill ingredients Various scientists have claimed that Gano Coffee is totally proven.

  • Pingback: Spring Tutorial()

  • Pingback: Spring Auto-Wiring Beans with @Autowired annotation()

  • syed

    Hi,

    I have a scary problem. In My app the User bean is Autowired and its used as the principle logged in user (So the user is not coming as a static bean from an xml) If there are ten users logged in I will have ten candidates for the @AutoWired User field. (right?) and I can get any one of them.
    tell me if I am wrong on this. and how to actually solve it if possible. My idea is that we shudnt @AutoWired any User info

    Basically, (If I am wrong in my assumption) I think that when you @Autowire a field, its looks into the container as whole and the container is not divided into subcontainers per session.

    Thanks