Understanding SpringBoot @SpringBootApplication, Core Annotations and the Startup Process
This article explains the composition of the @SpringBootApplication annotation, details the roles of @Configuration, @ComponentScan and @EnableAutoConfiguration, and walks through the SpringApplication.run execution flow and the underlying auto‑configuration mechanism that powers Spring Boot applications.
When creating any Spring Boot project, the entry point is a class annotated with @SpringBootApplication that contains a main method calling SpringApplication.run . The annotation itself is a composite of three core annotations: @Configuration , @EnableAutoConfiguration and @ComponentScan .
1. @Configuration
Marks the class as a Java‑based configuration source for the Spring IoC container, equivalent to XML <beans> . Methods annotated with @Bean inside such a class define bean definitions that are registered in the container.
2. @ComponentScan
Enables automatic scanning of components (e.g., @Component , @Repository ) within the package of the annotated class or specified base packages, loading them as beans.
3. @EnableAutoConfiguration
Triggers Spring Boot’s auto‑configuration by importing configuration classes listed in META-INF/spring.factories via @Import(EnableAutoConfigurationImportSelector.class) . It uses SpringFactoriesLoader to locate and instantiate these configuration classes based on classpath dependencies.
SpringApplication.run Execution Flow
The static run method creates a SpringApplication instance, initializes listeners, loads the Environment , determines the appropriate ApplicationContext (web or non‑web), prepares the context, and finally calls refresh() to instantiate beans. Throughout the process, various SpringApplicationRunListener callbacks are invoked (started, environmentPrepared, contextPrepared, contextLoaded, finished).
Auto‑Configuration Mechanism
Spring Boot scans all spring.factories files on the classpath, extracts factory class names, and uses reflection to instantiate them. These factories provide configuration classes that are conditionally applied using annotations such as @ConditionalOnClass , @ConditionalOnBean , and @ConditionalOnMissingBean . For example, the MyBatis starter defines MybatisAutoConfiguration which is activated only when SqlSessionFactory and a DataSource bean are present.
Key Code Samples
Typical Spring Boot starter class:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Equivalent explicit annotation usage:
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Auto‑configuration selector snippet:
@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration { ... }Overall, Spring Boot simplifies backend development by replacing verbose XML configuration with concise annotations and starter dependencies, allowing developers to focus on business logic while the framework handles bean registration, environment setup, and conditional auto‑configuration.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.