Bird
0
0

What will happen if two Beans of the same type are defined and @Autowired is used without qualifiers?

medium📝 Predict Output Q5 of 15
Spring Boot - Inversion of Control and Dependency Injection
What will happen if two Beans of the same type are defined and @Autowired is used without qualifiers?
ASpring merges both Beans into one
BSpring picks the first Bean silently
CSpring ignores the Beans and injects null
DSpring throws NoUniqueBeanDefinitionException at runtime
Step-by-Step Solution
Solution:
  1. Step 1: Understand @Autowired behavior with multiple Beans

    When multiple Beans of the same type exist, Spring cannot decide which to inject.
  2. Step 2: Identify Spring's response

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

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

    Multiple Beans no qualifier = Exception [OK]
Quick Trick: Multiple Beans need qualifiers or exception occurs [OK]
Common Mistakes:
  • Assuming Spring picks first Bean silently
  • Thinking Spring injects null instead of error
  • Believing Spring merges Beans automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes