HomeSpring FrameworkSpring @Qualifier annotation

Spring @Qualifier annotation

In this guide we discuss about Spring @Qualifier annotation and how and when to use @Qualifier in Spring application with examples.

1. Spring @Qualifier

Spring @Qualifier annotation is used to identify specific bean when you have several implementations of same type, this annotation is designed to be used to avoid ambiguity issues in injecting beans. Let’s see an example.

In following example, ContactDao and AddressDao implementing same type IDao.

public class ContactDao implements IDao {
  public void result() {
    System.out.println("Contact Dao Implementation");
public class AddressDao implements IDao {
  public void result() {
    System.out.println("Address Dao Implementation");

Wiring Beans: Note that @Qualifier commented to test how example works without annotating it.

public class AutowireByQualifierTestService {

	private IDao addressDao;
	private IDao contactDao;
	public void printResults() {
		System.out.println("\n ------ Autowire By Qualifier Results ------");


public class ApplicationAutoWiredDemo {
    public static void main(String[] args) {
    	ApplicationContext ctxt = new AnnotationConfigApplicationContext(AppConfigForAutowired.class);
        AutowireByQualifierTestService abqts = ctxt.getBean(AutowireByQualifierTestService.class);


Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.javabydeveloper.spring.autowire.dao.IDao' available: expected single matching bean but found 2: contact,address
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(

In the above example we got exception due to ambiguity of injecting beans into AutowireByQualifierTestService. Spring @Autowired does auto-wired beans by type, it works well if we have only one bean of same type, otherwise we have to use either Spring @Primary or Spring @Qualifier annotation to specify which bean needs to be injected. In above test services class remove the comments and run demo class again, there won’t be any error and results wold be following.

------ Autowire By Qualifier Results ------
Address Dao Implementation
Contact Dao Implementation

Also we use @Qualifier in injecting collections in Spring to inject specific beans of same type into collection.

2. Conclusion

In this tutorial, we learned about how @Qualifier helps to avoid ambiguity issues in injecting beans with examples.

3. References

  1. Spring Document
  2. Spring @Autowired Annotation
  3. Spring @Primary Annotation
  4. Spring Injecting Collections


Please enter your comment!
Please enter your name here