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.

@Component("contact")
public class ContactDao implements IDao {
  @Override
  public void result() {
    System.out.println("Contact Dao Implementation");
  }
}
@Component("address")
public class AddressDao implements IDao {
  @Override
  public void result() {
    System.out.println("Address Dao Implementation");
  }
}

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

@Component
public class AutowireByQualifierTestService {

	@Autowired
	//@Qualifier("address")
	private IDao addressDao;
	
	@Autowired
	//@Qualifier("contact")
	private IDao contactDao;
	
	public void printResults() {
		System.out.println("\n ------ Autowire By Qualifier Results ------");
		addressDao.result();
		contactDao.result();
	}
}

Testing:

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

Results:

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(DependencyDescriptor.java:220)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1285)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here