What is the use of @Qualifier annotation in Spring?

What is the use of @Qualifier annotation in Spring?

he use of @Qualifier is to support the @Autowired annotation when it needs help.

Typically, @Autowired can implicitly, automatically inject a Spring managed bean by type. In some cases (like the example above), you may have multiple beans implementing the same interface like Animal.

When two or more beans implement the same interface, @Qualifier is used to tell @Autowired what exactly to inject.

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);
}        

Next, let's create implementations - EmailService, SMSService, and TwitterService classes.

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);
    }
}        

In the above example, Dependency is injected by both setter and constructor so you can use either one of them.

We have used?@Qualifier to inject TwitterService bean using constructor injection:

    // setter based DI
    @Autowired
    @Qualifier("twitterService")
    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }        

We have used?@Qualifier to inject the TwitterService bean using setter injection:

    // constructor based DI
    @Autowired
    public MessageProcessorImpl(@Qualifier("twitterService") MessageService messageService) {
        this.messageService = messageService;
    }        

If you want to inject EmailService bean instead of TwitterService bean then you can simply pass bean EmailService bean name. For example:

    // constructor based DI
    @Autowired
    public MessageProcessorImpl(@Qualifier("emailService") MessageService messageService) {
        this.messageService = messageService;
    }        

AppConfiguration

Let's write the java based configuration.

@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

Let's test the example using the Spring IOC container which is an ApplicationContext object.

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 ");
    }
}        

Output:

twitter message sending         

Conclusion

In?this example, we have seen how to use?@Qualifier annotation?in conjunction with?@Autowired?to avoid confusion when we have two or more beans configured for the same type.

要查看或添加评论,请登录

Ahmed Abdelaziz的更多文章

  • Java 23

    Java 23

    New and updated Java language features, core API, and the JVM – Java 23 packs it all – for new Java developers to…

  • Kafka Producer And Consumer In Spring Boot

    Kafka Producer And Consumer In Spring Boot

    Appache Kafka is a distributed event streaming platform that is widely used for handling real-time data streams in…

  • Docker with Spring Boot in a simple way

    Docker with Spring Boot in a simple way

    This guide walks you through the process of building a Docker image for running a Spring Boot application. We start…

  • Quarkus Framework and Comparison with Spring Boot

    Quarkus Framework and Comparison with Spring Boot

    In this article, we’ll give an overview of the Quarkus framework and compare it with Spring Boot – the most popular…

  • Spring AI

    Spring AI

    Spring AI supports ChatGPT, the AI language model by OpenAI. ChatGPT has been instrumental in sparking interest in…

  • Aspect Oriented Programming and AOP in Spring Framework

    Aspect Oriented Programming and AOP in Spring Framework

    Aspect-Oriented Programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation…

    2 条评论
  • Spring Boot Caching with Redis

    Spring Boot Caching with Redis

    Spring Boot Cache Providers Cache providers allow us to transparently and clearly configure the cache in the…

  • The DispatcherServlet

    The DispatcherServlet

    The Engine of Request Handling in Spring Boot. 1.

  • Spring Data with MongoDB

    Spring Data with MongoDB

    1. Overview This article will be a quick and practical introduction to Spring Data MongoDB.

  • Transactionality

    Transactionality

    By default, methods inherited from inherit the transactional configuration from . For read operations, the transaction…

社区洞察

其他会员也浏览了