📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we will explore the differences between these annotations and demonstrate their use with practical examples.
@Qualifier Annotation
The @Qualifier annotation is used to resolve ambiguity when multiple beans of the same type are present in a Spring application context. It allows you to specify a specific bean to be injected by using a qualifier value.
The @Qualifier annotation is used in conjunction with @Autowired to avoid confusion when we have two or more beans configured for the same type. If there are multiple implementations for a single interface then we can use @Qualifier to choose the required implementation at runtime.
MessageService interface
public interface MessageService {
public void sendMsg(String message);
}
EmailService Class
public class EmailService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
SMSService Class
public class TwitterService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
TwitterService Class
public class SMSService implements MessageService{
public void sendMsg(String message) {
System.out.println(message);
}
}
MessageProcessor Interface and MessageProcessorImpl Class
It's time to see the usage of @Qualifier annotation.public interface MessageProcessor {
public void processMsg(String message);
}
public class MessageProcessorImpl implements MessageProcessor {
private MessageService messageService;
// setter based DI
@Autowired
@Qualifier("twitterService")
public void setMessageService(MessageService messageService) {
this.messageService = messageService;
}
// constructor based DI
@Autowired
public MessageProcessorImpl(@Qualifier("twitterService") MessageService messageService) {
this.messageService = messageService;
}
public void processMsg(String message) {
messageService.sendMsg(message);
}
}
// setter based DI @Autowired @Qualifier("twitterService") public void setMessageService(MessageService messageService) { this.messageService = messageService; }
// constructor based DI @Autowired public MessageProcessorImpl(@Qualifier("twitterService") MessageService messageService) { this.messageService = messageService; }
// constructor based DI @Autowired public MessageProcessorImpl(@Qualifier("emailService") MessageService messageService) { this.messageService = messageService; }
@Primary Annotation
@Component
@Primary
public class EmailSender implements MailSender {
// ...
}
@Component
public class SmsSender implements MailSender {
// ...
}
@Service
public class NotificationService {
@Autowired
private MailSender mailSender;
// ...
}
Comments
Post a Comment
Leave Comment