Bird
0
0

Why is it important to use @Qualifier instead of relying solely on @Primary when multiple beans of the same type exist?

hard📝 Conceptual Q10 of 15
Spring Boot - Inversion of Control and Dependency Injection
Why is it important to use @Qualifier instead of relying solely on @Primary when multiple beans of the same type exist?
A@Qualifier automatically creates beans at runtime
B@Primary disables other beans from being created
C@Qualifier allows explicit selection of a bean, avoiding ambiguity and improving clarity
D@Primary is deprecated and should not be used
Step-by-Step Solution
Solution:
  1. Step 1: Understand @Primary behavior

    @Primary marks one bean as default, but it may not cover all injection cases explicitly.
  2. Step 2: Benefits of @Qualifier

    @Qualifier explicitly specifies which bean to inject, improving code clarity and avoiding accidental injection of wrong bean.
  3. Final Answer:

    @Qualifier allows explicit selection of a bean, avoiding ambiguity and improving clarity -> Option C
  4. Quick Check:

    @Qualifier = explicit bean choice, better clarity [OK]
Quick Trick: Use @Qualifier for clear, explicit bean injection [OK]
Common Mistakes:
  • Thinking @Primary disables other beans
  • Believing @Qualifier creates beans
  • Assuming @Primary is deprecated

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes