Why Spring Boot Application class needs to have Configuration annotation?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Spring Boot application class does not need a separate @Configuration annotation because @SpringBootApplication already includes it. @SpringBootApplication is a composite annotation that combines @Configuration, @EnableAutoConfiguration, and @ComponentScan. If you see @Configuration on the main class alongside @SpringBootApplication, it is redundant — the configuration behavior is already active.
What @SpringBootApplication Contains
So @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan. The @Configuration part tells Spring that this class can define @Bean methods.
What @Configuration Does
@Configuration marks a class as a source of bean definitions. Spring processes the class with CGLIB proxying to ensure that @Bean methods return singleton instances — calling dataSource() from within userService() returns the same instance, not a new one.
Why You Do NOT Need Both
The @Bean method works because @SpringBootApplication already includes @Configuration. Adding @Configuration explicitly does no harm but adds confusion.
When to Use Separate @Configuration Classes
Separate @Configuration classes are the standard practice for organizing beans by concern. They are automatically detected by @ComponentScan (included in @SpringBootApplication).
CGLIB Proxying in @Configuration
Without @Configuration (using @Component instead), each call to sharedDependency() creates a new instance. This is the key behavioral difference — @Configuration ensures singleton semantics for inter-bean references.
@Configuration(proxyBeanMethods = false)
Spring Boot 2.2+ introduced proxyBeanMethods = false for faster startup. With this mode, you must inject dependencies through method parameters instead of calling other @Bean methods directly.
Common Pitfalls
- Adding
@Configurationalongside@SpringBootApplication: This is redundant since@SpringBootApplicationalready includes@Configurationvia@SpringBootConfiguration. It compiles and runs but misleads readers into thinking both are required. - Using
@Componentinstead of@Configurationfor bean definitions:@Componentclasses do not get CGLIB proxying, so calling one@Beanmethod from another creates a new instance each time instead of returning the singleton. Use@Configurationfor classes with inter-dependent@Beanmethods. - Putting all beans in the main application class: While technically valid, defining many
@Beanmethods in the@SpringBootApplicationclass creates a bloated entry point. Extract beans into focused@Configurationclasses organized by concern. - Forgetting that
@ComponentScanis included:@SpringBootApplicationscans the package of the main class and all sub-packages. Placing@Configurationclasses outside this package tree means they will not be detected without an explicit@ComponentScan(basePackages = ...). - Not understanding
proxyBeanMethods = false: In lite mode, calling another@Beanmethod directly creates a new instance. Dependencies must be injected through method parameters. Mixing lite mode with direct method calls causes subtle singleton violations.
Summary
@SpringBootApplicationalready includes@Configuration— adding both is redundant@Configurationmarks a class as a source of@Beandefinitions with CGLIB proxy support- CGLIB proxying ensures that
@Beanmethod calls return singleton instances - Use separate
@Configurationclasses to organize beans by concern (database, security, messaging) @Configuration(proxyBeanMethods = false)disables CGLIB proxying for faster startup but requires parameter injection@Componentclasses with@Beanmethods do not get singleton inter-bean references — use@Configurationinstead

