Unveiling Spring Boot’s @SpringBootApplication: How It Powers Your App Startup
This article explains how the @SpringBootApplication annotation combines @Configuration, @EnableAutoConfiguration, and @ComponentScan, walks through the SpringApplication.run execution flow, and reveals the inner workings of Spring Boot’s auto‑configuration mechanism using SpringFactoriesLoader and META‑INF/spring.factories.
We usually start any Spring Boot project with a class annotated with @SpringBootApplication, which is a meta‑annotation composed of @Configuration, @EnableAutoConfiguration and @ComponentScan.
What @SpringBootApplication actually does
It is defined as a composite annotation:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication { ... }The three core annotations are @Configuration (or @SpringBootConfiguration), @EnableAutoConfiguration and @ComponentScan, so using @SpringBootApplication is equivalent to declaring all three separately.
1. @Configuration
Marks the class as a Java‑based configuration source for the Spring IoC container, replacing XML <beans> definitions. Methods annotated with @Bean produce bean instances that are registered with the container.
@Configuration
public class MockConfiguration {
// @Bean definitions
}2. @ComponentScan
Automatically scans the package of the annotated class (or the packages specified via basePackages) for @Component, @Service, @Repository, etc., and registers them as beans.
3. @EnableAutoConfiguration
Triggers Spring Boot’s auto‑configuration mechanism. It uses @Import to load configuration classes listed in META‑INF/spring.factories based on the classpath. For example, adding spring-boot-starter-web brings Tomcat and Spring MVC auto‑configuration.
@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration { ... }The selector reads spring.factories, loads candidate @Configuration classes, and registers them in the ApplicationContext.
SpringApplication execution flow
SpringApplication.runcreates a SpringApplication instance, initializes listeners, the environment, and the ApplicationContext, then calls refreshContext which loads auto‑configuration via SpringFactoriesLoader, registers beans, and finally invokes CommandLineRunner beans.
The overall startup structure can be divided into three parts: SpringApplication initialization, concrete startup logic (including listeners, environment, context creation), and the auto‑configuration module that loads configuration classes from spring.factories.
By relying on starters such as mybatis-spring-boot-starter or spring-boot-starter-web, all necessary dependencies and auto‑configuration classes are pulled in automatically, allowing developers to focus on business logic instead of XML configuration and complex dependency management.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
