Spring Boot Startup Process and Auto‑Configuration Explained
This article details the Spring Boot startup sequence, including the run() method, creation of listeners, environment loading, application context initialization, and the role of @EnableAutoConfiguration and related annotations in automatically configuring beans and starter components.
Interviewer: Explain the Spring Boot startup process.
Answer: The process begins by locating the run() method in the main class, which creates a SpringApplication instance before invoking run() .
Entering run() , a SpringApplicationRunListeners is created to start listening.
The ConfigurableEnvironment is loaded, and the Environment is added to the listeners.
The application context ( ConfigurableApplicationContext ) is created as the return object of run() .
Finally, refreshContext(context) creates the Spring container, handling starter auto‑configuration and bean instantiation.
Interviewer: Explain the principle of Spring Boot auto‑configuration.
Answer: The @EnableAutoConfiguration annotation triggers a scan of META-INF/spring.factories to locate configuration classes, which are then loaded into the Spring container.
Detailed Knowledge Points
Core Spring Boot Annotations
The @SpringBootApplication annotation is a composite of three annotations:
1. @Configuration : Marks the class as a source of bean definitions, similar to an XML configuration file.
2. @EnableAutoConfiguration : Enables automatic configuration based on declared dependencies, involving key annotations such as:
@AutoConfigurationPackage imports Registrar.class to scan and register components under the package of the main class.
Therefore, controllers, services, etc., should reside in packages at the same level as the main class.
@Import({AutoConfigurationImportSelector.class}) uses AutoConfigurationImportSelector to call getCandidateConfigurations() , which loads class names from META-INF/spring.factories via SpringFactoriesLoader.loadFactoryNames() .
3. @ComponentScan : Scans for beans annotated with @Component in the package of the main class ( ExammanagerApplication.class ) and registers them.
Detailed Startup Process (with Source Code)
1. The run() method is invoked, creating a new SpringApplication instance:
public static ConfigurableApplicationContext run(Class
[] primarySources, String[] args) {
return (new SpringApplication(primarySources)).run(args);
}2. In the constructor of SpringApplication , initial values are set for the instance.
3. After the constructor finishes, control returns to run() , which performs several key steps:
Creates SpringApplicationRunListeners and starts listening.
Loads the ConfigurableEnvironment (or StandardEnvironment for web deployments).
Adds the Environment to the listeners.
Creates the return object ConfigurableApplicationContext .
Calls prepareContext to bind listeners, environment, arguments, banner, etc., to the context.
Invokes refreshContext(context) , which loads spring.factories , instantiates beans, and applies starter auto‑configurations (e.g., MyBatis, Redis).
After configuration, Spring Boot performs finalization steps and returns the application context, completing the startup sequence.
Thank you for reading; hope this helps :)
Source: blog.csdn.net/weixin_46047193/article/details/123557570
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.