Bird
0
0

Which combination of annotations should you use on your configuration class and bean method?

hard📝 Application Q15 of 15
Spring Boot - Advanced Patterns
You want to create a custom auto-configuration that only creates a bean if no other bean of the same type exists and a property app.feature.enabled is true. Which combination of annotations should you use on your configuration class and bean method? A) @Configuration on class, @ConditionalOnMissingBean and @ConditionalOnProperty on bean method B) @Component on class, @ConditionalOnBean on bean method C) @Configuration on class, @ConditionalOnClass on bean method D) @EnableAutoConfiguration on class, no conditions on bean method
A@Configuration on class, @ConditionalOnClass on bean method
B@Component on class, @ConditionalOnBean on bean method
C@Configuration on class, @ConditionalOnMissingBean and @ConditionalOnProperty on bean method
D@EnableAutoConfiguration on class, no conditions on bean method
Step-by-Step Solution
Solution:
  1. Step 1: Identify class-level annotation for config

    @Configuration is correct to mark a class as configuration for auto-configuration.
  2. Step 2: Choose bean method conditions

    @ConditionalOnMissingBean ensures no existing bean of the type exists; @ConditionalOnProperty checks the property is true.
  3. Step 3: Exclude incorrect options

    @Component is generic, @ConditionalOnBean requires existing bean, @ConditionalOnClass checks class presence, not bean existence, @EnableAutoConfiguration is for main app class.
  4. Final Answer:

    @Configuration on class, @ConditionalOnMissingBean and @ConditionalOnProperty on bean method -> Option C
  5. Quick Check:

    Missing bean + property conditions = @Configuration on class, @ConditionalOnMissingBean and @ConditionalOnProperty on bean method [OK]
Quick Trick: Use @ConditionalOnMissingBean and @ConditionalOnProperty together [OK]
Common Mistakes:
  • Using @ConditionalOnBean instead of @ConditionalOnMissingBean
  • Putting conditions on class instead of bean method
  • Confusing @EnableAutoConfiguration with config class annotation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes