@Qualifier for Qualifying bean
Using @Primary annotation you can specify the priority for the bean but when there are multiple then you still have the ambiguity then you can get the qualifying bean using the @Qualifier annotation.
Using @Qualifier annotation is you can work with qualifiers.
It can be used with the @Autowired or @Inject , to specify at the point of injection which
bean you wish to be injected.
If you want Mango bean is injected into setIceCream(IceCream iceCream):
@Autowired
@Qualifier("mango")
public void setIceCream(IceCream iceCream) {
this.iceCream= iceCream;
}
The parameter given in the @Qualifier is the ID of the bean.
Bean of all the @Component annotated classes will be create with ID as a uncapitalized class name.
There @Qualifier("mango") refers to the Mango class.
But here if you refactor the Mango class then it can create problem here.
So, in that case you can specify the @Qualifier with the @Bean annotation.
@Autowired
@Qualifier("myMango")
public void setIceCream(IceCream iceCream) {
this.iceCream= iceCream;
}
In the mango class you can have this
@Bean
@Qualifier("myMango")
public Icecream iceCream() {
return new Mango();
}