Addressing ambiguity in autowiring
Autowiring work properly when there is only one matching Bean available. If there are multiple Bean of the same requirement available then spring throws exception. This the ambiguity stops Spring from autowiring the property, method argument or constructor argumen.
suppose you’ve annotated the following setIceCream() method with @Autowired:
@Autowired
public void setIceCream(IceCream iceCream) {
this.iceCream= iceCream;
}
Here IceCream is an Interface and it is implemented by three Classes
Vanilla, Chocolate, Mango:
@Component
public class Vanilla implements IceCream { ... }
@Component
public class Mango implements IceCream { ... }
@Component
public class Chocolate implements IceCream { ... }
All three uses the @Component so, all will be picked up by spring during component scanning
and spring creates the bean in the Application Context. and when spring tries to autowire in the
setIceCream method then it has three choices there and it become confuse over there and it throw
NoUniqueBeanDefinitionException.
though spring provide options for resolving this ambiguity. You can make the bean primary.