Home  >  Spring Core

Spring @Qualifier Annotation Example using @Autowired

By Arvind Rai, February 12, 2013
In this page we will provide spring @Qualifier annotation example using @Autowired. @Qualifier annotation is used on the field or parameter level. It defines a qualifier for autowiring. The use of @Qualifier is to fix that the bean with a given name will only qualify for dependency injection in autowiring. In the scenario where more than one bean is eligible to be autowired, we can use @Qualifier annotation. The same scenario can also be handled by @Primary annotation. @Qualifier defines more robust selection criteria than @Primary annotation. Find the link for @Primary annotation example. The @Qualifier is defined at constructor argument level as follows.
@Autowired
public AnimalService(@Qualifier("deer") Animal animal, Zoo zoo){
	this.animal = animal;
	this.zoo = zoo;
} 
Find the sample code to use @Qualifier at field level.
@Autowired
@Qualifier("add")
private Address address; 
To understand how to create custom qualifier, find the link. Now we will discuss here the complete example of @Qualifier with @Autowired annotation.


Create Component using @Component

Find the classes being used in our example.
Animal.java
package com.concretepage.bean;
public interface Animal {
	void printName();
} 
Deer.java
package com.concretepage.bean;
import org.springframework.stereotype.Component;
@Component(value="deer")
public class Deer implements Animal {
	@Override
	public void printName() {
		System.out.println("---  Deer  ---");
	}
} 
Fox.java
package com.concretepage.bean;
import org.springframework.stereotype.Component;
@Component(value="fox")
public class Fox implements Animal {
	@Override
	public void printName() {
		System.out.println("---  Fox  ---");
	}
} 
Zoo.java
package com.concretepage.bean;
import org.springframework.stereotype.Component;
@Component
public class Zoo {
	public String getZooName() {
		return "ABC Zoo";
	}
} 
We have assigned the value in @Component as @Component(value="fox") and @Component(value="deer") where fox and deer will be considered as bean id.

Create Service with @Qualifier and @Autowired

Find a service class in which our one of constructor parameters is marked with @Qualifier.
AnimalService.java
package com.concretepage.bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class AnimalService {
	private Animal animal;
	private Zoo zoo;
	@Autowired
	public AnimalService(@Qualifier("deer") Animal animal, Zoo zoo){
		this.animal = animal;
		this.zoo = zoo;
	}
	public Animal getAnimal() {
		return animal;
	}
	public Zoo getZoo() {
		return zoo;
	}
} 
For the argument animal in our constructor, two beans are eligible for dependency injection i.e. deer and fox. Using @Qualifier, we are telling to spring container that use deer bean.

Configuration Class

Find the configuration class with @ComponentScan that will scan our classes annotated with @Component and @Service.
AppConfig.java
package com.concretepage;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages="com.concretepage.bean")
public class AppConfig {
} 

Run Main Method

Run the demo using the below class.
SpringDemo.java
package com.concretepage;
import java.sql.SQLException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.concretepage.bean.AnimalService;
public class SpringDemo {
	public static void main(String[] args) throws SQLException {
	    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	    ctx.register(AppConfig.class);
	    ctx.refresh();
	    AnimalService animal = ctx.getBean(AnimalService.class);
	    animal.getAnimal().printName();
	    System.out.println(animal.getZoo().getZooName());
    	    ctx.registerShutdownHook();
	}
}  
Find the output.
---  Deer  ---
ABC Zoo 
To test if we will not use @Qualifier, what will happen, just remove the @Qualifier annotation from constructor in AnimalService class and run the application. We will get error as follows.
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.concretepage.bean.Animal] is defined: expected single matching bean but found 2: deer,fox
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) 
For

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us