Category : Interview Questions | Sub Category : Spring Boot Interview Questions | By Prasad Bonam Last updated: 2023-08-14 11:14:30 Viewed : 565
In Spring, the @Qualifier
annotation is used to resolve ambiguities when you have multiple beans of the same type and you want to specify which bean should be injected. This is especially useful when you have a circular dependency situation or when there are multiple candidates for autowiring.
Here is an example of how to use the @Qualifier
annotation in Spring:
Suppose you have an interface Animal
and two implementations of that interface: Dog
and Cat
.
javapublic interface Animal {
void makeSound();
}
@Component
@Qualifier("dog")
public class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
@Component
@Qualifier("cat")
public class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow!");
}
}
Now, lets say you have a class PetOwner
that needs to inject an Animal
bean, but you have multiple candidates available. You can use the @Qualifier
annotation to specify which bean you want to inject:
java@Component
public class PetOwner {
private final Animal animal;
@Autowired
public PetOwner(@Qualifier("dog") Animal animal) {
this.animal = animal;
}
public void playWithPet() {
animal.makeSound();
}
}
In this example, the @Qualifier("dog")
annotation is used on the constructor parameter of the PetOwner
class to indicate that you want to inject the Dog
bean specifically.
If you didn`t use the @Qualifier
annotation, Spring would not know which bean to inject and would likely result in an error due to the ambiguity.
Remember that the string you provide to @Qualifier
should match the value specified in the @Qualifier
annotation on the beans you want to distinguish. Using @Qualifier
helps Spring to unambiguously identify the bean to be injected, especially when multiple candidates of the same type are available in the application context.
The @Qualifier
annotation in Spring is used to specify which bean should be injected when there are multiple beans of the same type in the context. It helps resolve ambiguity when the container cannot determine which bean to inject based solely on type information. Here is an example of how to use the @Qualifier
annotation:
Suppose you have an interface PaymentService
and two implementations of it: CreditCardPaymentService
and PayPalPaymentService
. You want to inject one of these implementations into another bean, and you have multiple beans of the same type. Here is how you can use @Qualifier
:
PaymentService
interface and its implementations:javapublic interface PaymentService {
void processPayment();
}
@Service
public class CreditCardPaymentService implements PaymentService {
public void processPayment() {
// Credit card payment logic
}
}
@Service
public class PayPalPaymentService implements PaymentService {
public void processPayment() {
// PayPal payment logic
}
}
PaymentService
, use the @Autowired
annotation with @Qualifier
to specify which implementation to inject:java@Service
public class OrderService {
private final PaymentService paymentService;
@Autowired
public OrderService(@Qualifier("creditCardPaymentService") PaymentService paymentService) {
this.paymentService = paymentService;
}
// ...
}
In this example, the @Qualifier("creditCardPaymentService")
annotation explicitly tells Spring which bean to inject. Here, "creditCardPaymentService"
corresponds to the bean name defined in the Spring context. If you dont use @Qualifier
, Spring might not know which bean to inject, especially when there are multiple beans of the same type.
java@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }}
In this example, the @ComponentScan
annotation scans the specified package for components (such as the PaymentService
implementations) to register them with the Spring context.
By using the @Qualifier
annotation, you can provide a clear indication to Spring about which bean to inject when dealing with multiple beans of the same type. This helps avoid ambiguity and ensures that the correct bean is injected based on your applications requirements.