Bird
0
0

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:
  1. Step 1: Identify correct annotation for configuration class

    Custom auto-configuration classes must be annotated with @Configuration to define beans properly.
  2. 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.
  3. Final Answer:

    @Configuration public class MyAutoConfig { @Bean public MyService myService() { return new MyService(); } } -> Option A
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes