Bird
0
0

Why does this bean fail to create?

medium📝 Debug Q7 of 15
Spring Boot - Advanced Patterns
Why does this bean fail to create?
@Bean
@ConditionalOnBean(name = "missingBean")
public MyBean myBean() { return new MyBean(); }

Assuming no bean named "missingBean" exists in the context.
ABecause @ConditionalOnBean requires a class, not a name
BBecause @Bean annotation is missing
CBecause the required bean named "missingBean" does not exist
DBecause the method return type is incorrect
Step-by-Step Solution
Solution:
  1. Step 1: Understand @ConditionalOnBean with name attribute

    The annotation checks if a bean with the given name exists in the context.
  2. Step 2: Analyze context state

    No bean named "missingBean" exists, so the condition fails and the bean is not created.
  3. Final Answer:

    Because the required bean named "missingBean" does not exist -> Option C
  4. Quick Check:

    @ConditionalOnBean(name) requires bean presence by name [OK]
Quick Trick: Bean name must exist for @ConditionalOnBean(name) [OK]
Common Mistakes:
  • Assuming @ConditionalOnBean only accepts classes
  • Expecting bean creation without required bean
  • Ignoring bean name case sensitivity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes