Do you know how the SpringBoot project started

Overview

SpringBoot is a new open source lightweight framework that was developed by the Pivotal team in 2013 and the first version was released in April 2014. It is designed based on Spring 4.0, which not only inherits the original excellent features of the Spring framework, but also further simplifies the entire construction and development process of Spring applications by simplifying the configuration. In addition, SpringBoot integrates a large number of frameworks so that the version conflicts of dependent packages and the instability of references have been well resolved.

Those who have used Spring Boot should know that in the main class main() method of the project startup entry, there is a simple sentence

SpringApplication.run( ... );

This opened the way to start the project.

So in this article we will take a look at what the hell is this SpringApplication and run() method, and what are the mysteries hidden behind it?

SpringApplication

The SpringApplication class should be regarded as an "innovative" product of the Spring Boot framework itself, because the original Spring framework does not have this class. SpringApplication encapsulates a set of Spring application startup procedures, but this is completely transparent to users, so we get started with Spring It feels simple and lightweight when booting.

Generally speaking, the default SpringApplication execution process can already meet most of the needs, but if the user wants to intervene in this process, they can expand the process through the extension points opened by SpringApplication in some parts of the process. The typical extension scheme is to use set method.

Let's take an example, disassemble and write out the startup class of Spring Boot application that we are used to every day:

@SpringBootApplication
public class FrontNovelApplication {
    
    
    
    public static void main(String[] args) {
    
    
        // 这是传统Spring Boot应用的启动,一行代码搞定,内部默认做了很多事
        SpringApplication.run(FrontNovelApplication.class);

        // 用户自定的扩展在此 !!!
        SpringApplication app = new SpringApplication(FrontNovelApplication.class);
        //app.setXXX();这里能做很多自定义的事情。
        app.setBanner((environment, sourceClass, out) -> out.println(100));
        app.run(args);
    }

After such a disassembly, we found that we also need to construct a SpringApplication class object first, and then call the object's run() method.

Then, let’s talk about the construction process of SpringApplication and the process of its run() method. If this is clear, then it is clear how the Spring Boot application runs.

Initialization of SpringApplication instance

Let's look at the code first:
Insert picture description here

The red boxes in the figure indicate four key steps, which are explained as follows:

① Infer the type of application: create one of REACTIVE application, SERVLET application, NONE
Insert picture description here

② Use SpringFactoriesLoader to find and load all available ApplicationContextInitializers in the META-INF/spring.factories file under the classpath
Insert picture description here

③ Use SpringFactoriesLoader to find and load all available ApplicationListeners in the META-INF/spring.factories file under the classpath
Insert picture description here

④ Infer and set the definition class of the main method
Insert picture description here

Exploring the run() method of SpringApplication

First look at what the code looks like:
Insert picture description here

We summarize and refine the steps as follows:

1. Load the META-INF/spring.factories file through SpringFactoriesLoader, obtain and create a SpringApplicationRunListener object

2. Then SpringApplicationRunListener sends out the starting message

3. Create parameters and configure the Environment that the current SpringBoot application will use

4. After completion, the environmentPrepared message is still sent by SpringApplicationRunListener

5. Create ApplicationContext

6. Initialize ApplicationContext, set Environment, load related configuration, etc.

7. The contextPrepared message is sent by SpringApplicationRunListener to inform that the ApplicationContext used by the Spring Boot application is ready OK

8. Load various beans into ApplicationContext, continue to send contextLoaded message by SpringApplicationRunListener to inform Spring Boot application that the ApplicationContext used by Spring Boot application has been filled with OK

9.refresh ApplicationContext, complete the last step available for IoC container

10. The started message is sent by SpringApplicationRunListener

11. Call the callRunners(...) method to execute the run method that implements the ApplicationRunner and CommandLineRunner interface classes. It is used to perform some additional operations after the Spring application context is prepared. So as to complete the start of the final program.

12. The running message is sent by SpringApplicationRunListener to inform that the program is running

At this point, the whole process is over.

Guess you like

Origin blog.csdn.net/m0_46864744/article/details/111714526