Bird
0
0

Consider this bean definition:

medium📝 Debug Q14 of 15
Spring Boot - Advanced Patterns
Consider this bean definition:
@Bean
@ConditionalOnProperty(name = "service.enabled", havingValue = "true")
public Service service() {
    return new Service();
}

Why does the bean never get created even when service.enabled=true is set in application.properties?
AThe property key name is incorrect or misspelled
BThe property is a string but havingValue expects a boolean
CThe bean method is missing @Component annotation
DThe bean method must be static to work with @ConditionalOnProperty
Step-by-Step Solution
Solution:
  1. Step 1: Verify property key spelling

    If the property key in application.properties is misspelled or different, condition fails.
  2. Step 2: Check other options

    HavingValue compares strings, so boolean type mismatch is not an issue; @Component is not needed on @Bean methods; static is not required.
  3. Final Answer:

    The property key name is incorrect or misspelled -> Option A
  4. Quick Check:

    Property key must match exactly [OK]
Quick Trick: Check property key spelling carefully for conditional beans [OK]
Common Mistakes:
  • Assuming boolean type mismatch causes failure
  • Thinking @Component is needed on @Bean methods
  • Believing bean methods must be static

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes