Bird
0
0

Identify the error in this custom auto-configuration class:

medium📝 Debug Q14 of 15
Spring Boot - Advanced Patterns
Identify the error in this custom auto-configuration class:
@Configuration
@ConditionalOnClass(name = "com.example.Service")
public class ServiceAutoConfiguration {
    @Bean
    public Service service() {
        return new Service();
    }

    @Bean
    public Service service() {
        return new Service();
    }
}
AMissing @ConditionalOnProperty annotation
BNo error, code is valid
CService class not imported
DDuplicate bean method names causing a compilation error
Step-by-Step Solution
Solution:
  1. Step 1: Check bean method definitions

    Two methods named service() exist, both annotated with @Bean, causing duplicate bean names.
  2. Step 2: Understand Spring bean naming rules

    Bean names must be unique; duplicate method names cause compilation or context startup errors.
  3. Final Answer:

    Duplicate bean method names causing a compilation error -> Option D
  4. Quick Check:

    Duplicate @Bean methods = error [OK]
Quick Trick: Bean method names must be unique in config classes [OK]
Common Mistakes:
  • Ignoring duplicate method names
  • Thinking missing @ConditionalOnProperty is an error here
  • Assuming missing import causes this error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes