Make a bean Primary using @Primary
When there are multiple bean available for same requirement then it creates the ambiguity.
You can resolve this using the @Primary annotation.
@Primary annotated bean will be given the highest priority.
Here IceCream is an Interface and it is implemented by three Classes
Vanilla, Chocolate, Mango:
@Component
public class Vanilla implements IceCream { ... }
If Mango is your favourite IceCream then you can make it primary
@Component
@Primary
public class Mango implements IceCream { ... }
@Component
public class Chocolate implements IceCream { ... }
Or, if you declare the IceCream bean explicitly in Java configuration, the @Bean method will be this:
@Bean
@Primary
public IceCream mangoIceCream() {
return new Mango();
}
In XML:
<bean id="iceCream"
class="com.desserteater.IceCream"
primary="true" />
Where there are multiple primary beans then there is again ambiguity. Spring cannot choose from
multiple primary beans. So, when there are multiple primary beans then there are no primary bean.