Sometimes Spring finds more than one bean of the same type and doesn't know which one to use. @Qualifier helps tell Spring exactly which bean to pick.
@Qualifier for ambiguous beans in Spring Boot
@Autowired
@Qualifier("beanName")
private BeanType bean;@Qualifier uses the bean's name to pick the right one.
You put @Qualifier right where you inject the bean.
Service. @Qualifier("fastService") tells Spring to use the FastService bean.@Component("fastService") public class FastService implements Service {} @Component("slowService") public class SlowService implements Service {} @Autowired @Qualifier("fastService") private Service service;
@Qualifier with @Bean methods to pick the right greeting bean.@Bean(name = "englishGreeting") public Greeting englishGreeting() { return new EnglishGreeting(); } @Bean(name = "spanishGreeting") public Greeting spanishGreeting() { return new SpanishGreeting(); } @Autowired @Qualifier("spanishGreeting") private Greeting greeting;
This Spring Boot app has two Printer beans. Using @Qualifier("consolePrinter") tells Spring to use the ConsolePrinter bean. When run, it prints a message from the chosen bean.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; interface Printer { void print(); } @Component("consolePrinter") class ConsolePrinter implements Printer { public void print() { System.out.println("Printing to console"); } } @Component("filePrinter") class FilePrinter implements Printer { public void print() { System.out.println("Printing to file"); } } @SpringBootApplication public class QualifierExample implements CommandLineRunner { @Autowired @Qualifier("consolePrinter") private Printer printer; public static void main(String[] args) { SpringApplication.run(QualifierExample.class, args); } @Override public void run(String... args) { printer.print(); } }
If you forget @Qualifier when multiple beans exist, Spring throws an error about ambiguity.
You can use @Qualifier on constructor parameters, fields, or setter methods.
Bean names are case-sensitive when used with @Qualifier.
@Qualifier helps Spring pick the right bean when there are many of the same type.
Use @Qualifier("beanName") where you inject the bean.
This avoids errors and makes your code clearer.
Practice
@Qualifier in Spring Boot?Solution
Step 1: Understand bean ambiguity in Spring
When multiple beans of the same type exist, Spring cannot decide which one to inject automatically.Step 2: Role of @Qualifier
@Qualifier explicitly tells Spring which bean to use by specifying its name.Final Answer:
To specify which bean to inject when multiple beans of the same type exist -> Option BQuick Check:
@Qualifier = Select specific bean [OK]
- Thinking @Qualifier creates beans
- Confusing @Qualifier with @Component
- Using @Qualifier for unrelated tasks
@Qualifier to inject a bean named "serviceA"?Solution
Step 1: Check syntax for @Qualifier
@Qualifier requires the bean name as a string literal, so it must be in double quotes.Step 2: Identify correct option
@Autowired @Qualifier("serviceA") private Service service; uses @Qualifier("serviceA") which is the correct syntax in Java annotations.Final Answer:
@Autowired @Qualifier("serviceA") private Service service; -> Option DQuick Check:
String bean name in quotes = correct syntax [OK]
- Omitting quotes around bean name
- Using single quotes instead of double quotes
- Missing access modifier on field
useService() is called?@Component("serviceA")
class ServiceA implements Service { public String getName() { return "A"; } }
@Component("serviceB")
class ServiceB implements Service { public String getName() { return "B"; } }
@Autowired
@Qualifier("serviceB")
private Service service;
public void useService() {
System.out.println(service.getName());
}Solution
Step 1: Identify injected bean by @Qualifier
@Qualifier("serviceB") tells Spring to inject the bean named "serviceB", which is ServiceB.Step 2: Check method output
ServiceB's getName() returns "B", so printing service.getName() outputs "B".Final Answer:
B -> Option AQuick Check:
@Qualifier("serviceB") injects ServiceB = prints B [OK]
- Assuming default bean is injected without @Qualifier
- Confusing bean name with class name
- Expecting compilation error due to multiple beans
@Component("serviceA")
class ServiceA implements Service {}
@Autowired
@Qualifier("serviceB")
private Service service;Solution
Step 1: Check available beans
Only a bean named "serviceA" is defined; no bean named "serviceB" exists.Step 2: Effect of @Qualifier("serviceB")
@Qualifier("serviceB") asks Spring to inject a bean named "serviceB", which is missing, causing an error.Final Answer:
No bean named 'serviceB' found for injection -> Option AQuick Check:
@Qualifier name must match existing bean [OK]
- Assuming interface needs @Component
- Ignoring missing bean name mismatch
- Thinking @Autowired alone resolves ambiguity
PaymentService: @Component("paypalService") and @Component("stripeService"). You want to inject stripeService but only want to use constructor injection. Which is the correct way to do this?Solution
Step 1: Understand constructor injection with @Qualifier
To inject a specific bean via constructor, place @Qualifier on the constructor parameter.Step 2: Identify correct code
public class OrderProcessor { private final PaymentService paymentService; public OrderProcessor(@Qualifier("stripeService") PaymentService paymentService) { this.paymentService = paymentService; } } correctly uses @Qualifier("stripeService") on the constructor parameter, ensuring the right bean is injected.Final Answer:
Constructor with @Qualifier on parameter -> Option CQuick Check:
Constructor param @Qualifier picks bean [OK]
- Using field injection when constructor injection is required
- Missing @Qualifier on constructor parameter
- Confusing setter injection with constructor injection
Ready to prove it?
Pass the test and this pattern moves unknown
