Bird
0
0

Why might you use @ConditionalOnMissingBean instead of @ConditionalOnBean when defining a bean?

hard📝 Conceptual Q10 of 15
Spring Boot - Advanced Patterns
Why might you use @ConditionalOnMissingBean instead of @ConditionalOnBean when defining a bean?
ATo create a bean only if another bean exists, ensuring dependencies
BTo create a bean only if a property is set
CTo always create a bean regardless of other beans
DTo create a bean only if no other bean of the same type exists, avoiding duplicates
Step-by-Step Solution
Solution:
  1. Step 1: Understand @ConditionalOnMissingBean purpose

    This annotation creates a bean only if no other bean of the same type is present, preventing duplicate beans.
  2. Step 2: Contrast with @ConditionalOnBean

    @ConditionalOnBean creates a bean only if another bean exists, which is the opposite behavior.
  3. Final Answer:

    To create a bean only if no other bean of the same type exists, avoiding duplicates -> Option D
  4. Quick Check:

    @ConditionalOnMissingBean avoids duplicate beans [OK]
Quick Trick: Use missing bean condition to avoid duplicates [OK]
Common Mistakes:
  • Confusing missing bean with existing bean condition
  • Assuming it always creates the bean
  • Mixing property conditions with bean presence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes