Spring @Qualifier Annotation Example
In this section we will learn about @Qualifier Annotation.
The @Qualifier
annotation is used to resolve the autowiring conflict, when there are multiple beans of same type.
If two or more beans of same type declared in the configuration file then autowiring conflict will occur if you use only @Autowired.
Annotation based Configuration example:
Let's take a Message Processing Example - a message can be sent in many ways like email, SMS etc.
Create
MessageService
interface for multiple message service implementations - EmailService
, and SMSService
classes.public interface MessageService {
public void sendMessage();
}
Create implementations -
EmailService
, andSMSService
classes.@Component("emailService")
public class EmailService implements MessageService{
@Override
public void sendMessage() {
System.out.println("Sending Email");
}
}
@Component("smsService")
public class SMSService implements MessageService{
@Override
public void sendMessage() {
System.out.println("Sending SMS");
}
}
It's time to see the usage of
@Qualifier
annotation. We have used @Qualifier to inject SMSService bean using filed injection:@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
@Qualifier("smsService")
MessageService service;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
service.sendMessage();
}
}
Java based Configuration example:
Let's take a Payment Processing Example - a payment can be process in many ways like Paypal, Stripe etc.
Create
PaymentService
interface for multiple payment service implementations - PayPalService
, and StripeService
classes.public interface PaymentService {
public void processPayment();
}
Create implementations -
StripeService
, andPaypalService
classes.public class StripeService implements PaymentService{
@Override
public void processPayment() {
System.out.println("Stripe payment processing");
}
}
public class PayPalService implements PaymentService{
@Override
public void processPayment() {
System.out.println("Paypal payment processing");
}
}
It's time to see the usage of
@Qualifier
annotation. We have used @Qualifier to inject StripeService bean using filed injection:@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
@Qualifier("stripeService")
PaymentService paymentService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
paymentService.processPayment();
}
}
The following example creates a Spring Boot web application which uses @Qualifier annotation.
Project Directory
Pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-qualifier-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-qualifier-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
MessageService.java
package com.knf.dev.demo.service;
public interface MessageService {
public void sendMessage();
}
SMSService.java
package com.knf.dev.demo.service;
import org.springframework.stereotype.Component;
@Component("smsService")
public class SMSService implements MessageService{
@Override
public void sendMessage() {
System.out.println("Sending SMS");
}
}
EmailService.java
package com.knf.dev.demo.service;
import org.springframework.stereotype.Component;
@Component("emailService")
public class EmailService implements MessageService{
@Override
public void sendMessage() {
System.out.println("Sending Email");
}
}
PaymentService.java
package com.knf.dev.demo.service;
public interface PaymentService {
public void processPayment();
}
StripeService.java
package com.knf.dev.demo.service;
public class StripeService implements PaymentService{
@Override
public void processPayment() {
System.out.println("Stripe payment processing");
}
}
PayPalService.java
package com.knf.dev.demo.service;
public class PayPalService implements PaymentService{
@Override
public void processPayment() {
System.out.println("Paypal payment processing");
}
}
AppConfig.java
package com.knf.dev.demo.config;
import com.knf.dev.demo.service.PaymentService;
import com.knf.dev.demo.service.PayPalService;
import com.knf.dev.demo.service.StripeService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean("payPalService")
public PaymentService paypalService()
{
return new PayPalService();
}
@Bean("stripeService")
public PaymentService stripeService()
{
return new StripeService();
}
}
Run Application - Application.java
package com.knf.dev.demo;
import com.knf.dev.demo.service.MessageService;
import com.knf.dev.demo.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
@Qualifier("smsService")
MessageService service;
@Autowired
@Qualifier("stripeService")
PaymentService paymentService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
service.sendMessage();
paymentService.processPayment();
}
}
Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.
Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:
mvn spring-boot:run
Console Output:
Sending SMS
Stripe payment processing
More related topics,