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

18 comments on “Spring Autowiring @Qualifier example

  1. 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

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

    Reply
  3. @Autowired
    private Person personA; will also works

    Reply
  4. 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?

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

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

    Thank you so much X 100000000…!
    😀 😀

    Reply
  7. Hi

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

    Reply
  8. 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.

    Reply
  9. 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

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

      Reply
  10. 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!

    Reply
  11. 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?

    Reply
  12. “To fix above problem, you need @Quanlifier”

    Reply
  13. 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

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

      Reply
  14. 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.

    Reply
  15. 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

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *