Bird
0
0

What will happen if two beans of the same type exist and one is injected without specifying a qualifier?

medium📝 component behavior Q5 of 15
Spring Boot - Inversion of Control and Dependency Injection
What will happen if two beans of the same type exist and one is injected without specifying a qualifier?
ASpring merges both beans into one
BSpring picks the first bean silently
CSpring ignores the injection and sets null
DSpring throws a NoUniqueBeanDefinitionException at startup
Step-by-Step Solution
Solution:
  1. Step 1: Understand bean ambiguity

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

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

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

    Multiple beans without qualifier = Exception thrown [OK]
Quick Trick: Multiple beans of same type need @Qualifier or exception occurs [OK]
Common Mistakes:
  • Assuming Spring picks a bean silently
  • Thinking Spring sets null on ambiguity
  • Believing Spring merges beans automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes