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 the Author

author image
mkyong
Founder of Mkyong.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

Leave a Reply

avatar
newest oldest most voted
Mathieu Gallo
Guest
Mathieu Gallo

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

Roberto Fonseca
Guest
Roberto Fonseca

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?

Brayan Loayza
Guest
Brayan Loayza

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

Thank you so much X 100000000…!
:D :D

Arka Dey
Guest
Arka Dey

Hi

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

Arka Dey
Guest
Arka Dey

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.

venkata raghava
Guest
venkata raghava

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

Juan Franzoi
Guest
Juan Franzoi

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
Guest
Dave Cahill

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
Guest
just spelling issue

“To fix above problem, you need @Quanlifier”

Himkar Dwivedi
Guest
Himkar Dwivedi

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
Guest
Alex

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

which diet pills work the best
Guest
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.

trackback
Spring Tutorial

[…] Spring Autowiring @Qualifier example Example to identify which bean is qualified to autowired on a field. […]

trackback
Spring Auto-Wiring Beans with @Autowired annotation

[…] It means, bean “PersonBean1″ is autowired into the Customer’s person property. Read this full example – Spring Autowiring @Qualifier example […]

syed
Guest
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… Read more »
arun kumar
Guest
arun kumar

@Autowired
private Person personA; will also works