📘 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
Spring @Qualifier Annotation Overview
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.
YouTube Video - @Qualifier Annotation
Spring @Qualifier Annotation Example
Let's take a Message Processing Example - a message can be sent in many ways like Email, SMS, Twitter, etc.Let's create a MessageService interface for multiple message service implementations - EmailService, SMSService, and TwitterService classes.
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; }
AppConfiguration
@Configuration
@ComponentScan("com.javadevsguide.springframework.di")
public class AppConfiguration {
@Bean(name="emailService")
public MessageService emailService(){
return new EmailService();
}
@Bean(name="twitterService")
public MessageService twitterService(){
return new TwitterService();
}
@Bean(name="smsService")
public MessageService smsService(){
return new SMSService();
}
@Bean
public MessageProcessor messageProcessor(){
return new MessageProcessorImpl(twitterService());
}
}
Testing
public class TestApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
MessageProcessor processor = applicationContext.getBean(MessageProcessor.class);
processor.processMsg("twitter message sending ");
}
}
twitter message sending
Conclusion
Spring Framework Related Posts
- Guide to Dependency Injection in Spring
- Spring Dependency Injection via Setter Example
- Spring Dependency Injection via Constructor Example
- Guide to Spring Bean Scopes
- Singleton and Prototype Bean Scopes Examples
- Spring @Qualifier Annotation Example
- Spring Java-Based Configuration Basics
- Spring Java-Based Configuration Example
Comments
Post a Comment
Leave Comment