Bird
0
0

What will happen if two custom auto-configuration classes both define a bean of the same type without conditional annotations?

medium📝 component behavior Q5 of 15
Spring Boot - Advanced Patterns
What will happen if two custom auto-configuration classes both define a bean of the same type without conditional annotations?
ASpring Boot merges both beans automatically
BBoth beans will be created with different names
COnly the first bean will be created silently
DSpring Boot will fail to start due to bean definition conflict
Step-by-Step Solution
Solution:
  1. Step 1: Understand bean conflict in Spring Boot

    If two beans of the same type exist without conditions, Spring Boot cannot decide which to use and throws an error.
  2. Step 2: Evaluate options

    Options B and D are incorrect because Spring does not merge or silently ignore conflicts. Only the first bean will be created silently is incorrect because Spring fails fast.
  3. Final Answer:

    Spring Boot will fail to start due to bean definition conflict -> Option D
  4. Quick Check:

    Duplicate beans without conditions cause startup failure [OK]
Quick Trick: Avoid duplicate beans by using conditional annotations [OK]
Common Mistakes:
  • Assuming Spring merges duplicate beans
  • Expecting silent override without error
  • Ignoring conditional annotations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes