WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Intermediate · question 27 of 100

Explain the use of the @Qualifier annotation in Spring.?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

The ‘@Qualifier‘ annotation is used in Spring to distinguish between different instances of a bean type when there are multiple candidates available for autowiring by type.

Consider a scenario where we have multiple implementation classes of an interface, and we want to specify which one should be injected in a particular bean. In such cases, we can annotate the implementation classes with the ‘@Qualifier‘ annotation and provide a unique value to each one. We can then use this value in conjunction with the ‘@Autowired‘ annotation to specify which implementation should be used for injection.

For example, lets say we have an interface called ‘PaymentService‘ and two implementation classes ‘PaypalPaymentService‘ and ‘StripePaymentService‘. We can annotate these implementation classes with the ‘@Qualifier‘ annotation like this:

@Component
@Qualifier("paypal")
public class PaypalPaymentService implements PaymentService { ... }

@Component
@Qualifier("stripe")
public class StripePaymentService implements PaymentService { ... }

Now we can specify which implementation to use for injection as follows:

@Autowired
@Qualifier("paypal")
private PaymentService paymentService;

Here, we have used the ‘@Qualifier‘ annotation along with ‘@Autowired‘ to tell Spring to inject an instance of ‘PaypalPaymentService‘.

It’s important to note that ‘@Qualifier‘ annotations can be used in combination with ‘@Autowired‘ for both constructor and property-based dependency injection.

In summary, the ‘@Qualifier‘ annotation is a way to differentiate between multiple implementations of a bean type when using autowiring in Spring.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics