Why Spring Boot Application class needs to have Configuration annotation?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
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
Related reading
- Why spring boot generates jar or war file with .original extension?
- Why there is no ConcurrentHashSet against ConcurrentHashMap
- Why to use AllArgsConstructor and NoArgsConstructor together over an Entity?
- Why use a prime number in hashCode?
- Why use a ReentrantLock if one can use synchronized(this)?
- Why use a ReentrantLock if one can use synchronizedthis?
- Why use Gradle instead of Ant or Maven?
- Why use Optional.of over Optional.ofNullable?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.