Bird
0
0

What will happen if Spring finds multiple beans of the same type but @Autowired is used without qualifiers?

medium📝 Conceptual Q5 of 15
Spring Boot - Inversion of Control and Dependency Injection
What will happen if Spring finds multiple beans of the same type but @Autowired is used without qualifiers?
ASpring ignores the injection and sets null
BSpring throws NoUniqueBeanDefinitionException at runtime
CSpring picks the first bean silently
DSpring compiles with a warning but runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Understand Spring behavior with multiple beans

    If multiple beans of the same type exist, Spring cannot decide which to inject without qualifiers.
  2. Step 2: Identify the error thrown

    Spring throws NoUniqueBeanDefinitionException to indicate ambiguity.
  3. Final Answer:

    Spring throws NoUniqueBeanDefinitionException at runtime -> Option B
  4. Quick Check:

    Multiple beans without qualifier = NoUniqueBeanDefinitionException [OK]
Quick Trick: Multiple beans need @Qualifier or error occurs [OK]
Common Mistakes:
  • Thinking Spring picks first bean silently
  • Assuming null is injected on ambiguity
  • Believing compile-time warning occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes