Main Tutorials

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

About Author

author image
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

Subscribe
Notify of
18 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Arka Dey
6 years ago

Hi

The another query is, what if we need personA and personB both at that moment what we need to do.

venkata raghava
8 years ago

hi

i am getting error for above mentioned example can anyone pls help me

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [beans.Person] is defined: expected single matching bean but found 2: personA,personB

at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1061)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)

… 15 more

Ratna Kumar C
4 years ago

need to add this in bean configuration file context : annotation -config tag

arun kumar
5 years ago

@Autowired
private Person personA; will also works

Roberto Fonseca
6 years ago

Is there an way to inject a qualifier from a form field. For example, let the user choose the implementation he wants to use in the process?

Mathieu Gallo
6 years ago

It’s written “@quanlifier” under the “@Qualifier example”. Please delete this comment when you fixed it

Evan Ross
3 years ago

Doesn’t that defeat the point of injection? This forces the receiver to decide at compile time which version of Person it should receive.
What if the choice (PersonA vs PersonB) isn’t known until run time (command line option, property value, environment variable, etc)?
How would you go about doing it in the Spring @Configuration class?
I have an interface (PersonIf) and then two potential implementations: PersonA and PersonB

wandering_coder
4 years ago

found a typo ‘Quanlifier’, and if I may, would like to recommend a grammar checking plugin with all due respect.

Brayan Loayza
6 years ago

Bro you are the best…!, I can fix a issue with this line:

Thank you so much X 100000000…!
😀 😀

Arka Dey
6 years ago

Hi mkyong,

As you used here @qualifier to mention which bean object we need to inject.
But as you create two person object but inject personA, so if you know that you need to inject only personA then what is the use of @Qualifier at this point.

Juan Franzoi
9 years ago

Hi, great explanation and example.
I would like to know if it’s possible to determine at startup time which service should be injected (using @Quanlifier annotation), but the value/parameter must be recovered from a property file (mongodb).
Thanks in advance!

Dave Cahill
9 years ago

Compiling with Java 7, this example works perfectly. With Java 8, the @Qualifier annotation seems not to work:
expected single matching bean but found 2

Any idea why that is?

just spelling issue
10 years ago

“To fix above problem, you need @Quanlifier”

which diet pills work the best
10 years ago

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.

syed
12 years ago

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

Himkar Dwivedi
10 years ago

Hi following example is not working for me

@Autowired
@Qualifier(“autowireBase1”)
private AutowireBase autowireBase;

public class AutowireSub1Impl implements AutowireBase{}

public class AutowireSub2Impl implements AutowireBase {}

getting this error

ERROR: org.springframework.web.servlet.DispatcherServlet – Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘homeController’: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.hike.autowire.AutowireBase com.hike.init.HomeController.autowireBase; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.hike.autowire.AutowireBase] is defined: expected single matching bean but found 2: autowireBase1,autowireBase2

Alex
10 years ago
Reply to  Himkar Dwivedi

Use id=”autowireBase1″ instead of name=”autowireBase1″. Name and id are not equivalent.