0
0
Spring Bootframework~5 mins

@Qualifier for ambiguous beans in Spring Boot - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What problem does the @Qualifier annotation solve in Spring Boot?
It helps Spring know which bean to inject when there are multiple beans of the same type, avoiding confusion.
Click to reveal answer
beginner
How do you use @Qualifier to specify a bean named 'serviceA' for injection?
You add @Qualifier("serviceA") above the injection point, like: <br>@Autowired<br>@Qualifier("serviceA")<br>private MyService service;
Click to reveal answer
beginner
True or False: @Qualifier can be used with constructor injection, setter injection, and field injection.
True. @Qualifier works with all types of dependency injection to specify which bean to use.
Click to reveal answer
beginner
What happens if you have two beans of the same type but do NOT use @Qualifier when injecting?
Spring throws an error because it doesn't know which bean to pick. This is called an ambiguous bean error.
Click to reveal answer
intermediate
Can @Qualifier be combined with @Primary? How do they work together?
Yes. @Primary marks a default bean to use if no @Qualifier is specified. But if @Qualifier is used, it overrides @Primary.
Click to reveal answer
What does @Qualifier do in Spring Boot?
ASpecifies which bean to inject when multiple beans of the same type exist
BMarks a bean as the primary bean
CCreates a new bean instance
DDisables bean injection
If you have two beans of type Service, how do you avoid ambiguity?
AUse @ComponentScan
BUse @Autowired only
CUse @Qualifier to name the bean to inject
DRemove one bean
Which of these is a correct way to use @Qualifier with constructor injection?
Apublic MyClass(MyService service) {}
B@Qualifier("bean1") public MyClass(MyService service) {}
Cpublic MyClass(MyService service) {@Qualifier("bean1")}
Dpublic MyClass(@Qualifier("bean1") MyService service) {}
What annotation marks a bean as the default when no @Qualifier is used?
A@Primary
B@Default
C@Qualifier
D@Autowired
If a bean is marked with @Primary and another bean is injected with @Qualifier, which bean is used?
AThe @Primary bean
BThe bean specified by @Qualifier
CBoth beans
DInjection fails
Explain why @Qualifier is important when you have multiple beans of the same type in Spring Boot.
Think about how Spring chooses beans when there are many options.
You got /4 concepts.
    Describe how you would use @Qualifier in a Spring Boot project with two implementations of the same interface.
    Consider how to tell Spring which version to use.
    You got /4 concepts.