When several Spring beans match an injection point, Spring needs a way to choose. @Qualifier names the specific bean to inject, instead of leaving the choice ambiguous. For example, @Qualifier("parrot2") Parrot parrot2 selects the bean named parrot2; without guidance or a primary bean, Spring can fail because it found multiple matches. section 3.4 section 4.2.2
Use @Qualifier at the injection point to choose explicitly. Unlike @Primary, which marks a default bean, a qualifier lets different consumers select different implementations of the same interface. section 4.2.2 section 4.5
Listing 4.15 Specifying the implementation Spring needs to inject with @Qualifier
@Component
public class CommentService {
private final CommentRepository commentRepository;
private final CommentNotificationProxy commentNotificationProxy;
public CommentService( #A
CommentRepository commentRepository,
@Qualifier("PUSH") CommentNotificationProxy commentNotificationProxy) {
this.commentRepository = commentRepository;
this.commentNotificationProxy = commentNotificationProxy;
}
// Omitted code
}The constructor parameter’s @Qualifier("PUSH") directs Spring to inject the push-notification implementation. section 4.2.2
