Which of the following is the correct way to declare a custom auto-configuration class in Spring Boot?
easy📝 Syntax Q3 of 15
Spring Boot - Advanced Patterns
Which of the following is the correct way to declare a custom auto-configuration class in Spring Boot?
A@Configuration public class MyAutoConfig { @Bean public MyService myService() { return new MyService(); } }
B@Component public class MyAutoConfig { public MyService myService() { return new MyService(); } }
C@Service public class MyAutoConfig { @Bean public MyService myService() { return new MyService(); } }
Dpublic class MyAutoConfig { @Bean public MyService myService() { return new MyService(); } }
Step-by-Step Solution
Solution:
Step 1: Identify correct annotation for configuration class
Custom auto-configuration classes must be annotated with @Configuration to define beans properly.
Step 2: Check bean method and class annotations
@Configuration public class MyAutoConfig { @Bean public MyService myService() { return new MyService(); } } uses @Configuration and @Bean correctly. Options B and C use @Component or @Service which are not for configuration. public class MyAutoConfig { @Bean public MyService myService() { return new MyService(); } } lacks any class-level annotation.
Final Answer:
@Configuration public class MyAutoConfig { @Bean public MyService myService() { return new MyService(); } } -> Option A
Quick Check:
Auto-config class = @Configuration + @Bean methods [OK]
Quick Trick:Use @Configuration on auto-config classes with @Bean methods [OK]
Common Mistakes:
Using @Component or @Service instead of @Configuration
Forgetting @Configuration annotation
Missing @Bean on factory methods
Master "Advanced Patterns" in Spring Boot
9 interactive learning modes - each teaches the same concept differently