By CodeSheep mp.weixin.qq.com/s/-MYQ-meSU…

We mentioned

In the Java backend development world, the famous Spring Boot framework must be used by everyone.

As anyone who has used Spring Boot knows, in the main() method of the project launch portal, there is a simple sentence

SpringApplication.run( ... ) ;Copy the code

Then the project started to run.

In this article, we’ll take a look at the SpringApplication and the run() method, and what’s behind it.

SpringApplication

The SpringApplication class should be regarded as the “innovation” 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 process, However, this is completely transparent to the user, so when we started Spring Boot, it felt clean and lightweight.

In general, the default SpringApplication execution process already meets most requirements, but if users want to interfere with the process, they can extend the process through extension points that SpringApplication opens somewhere in the process. The typical extension is to use the set method.

Spring Boot = Spring Boot = Spring Boot = Spring Boot = Spring Boot

@SpringBootApplicationpublic class CodeSheepApplication { public static void main( String[] args ) { // SpringApplication.run( CodeSheepApplication.class args ); / / this is a traditional Spring Boot application startup, one line of code, internal default did many things SpringApplication app = new SpringApplication (CodeSheepApplication. Class); app.setXXX( ... ) ; // User defined extensions are here!! app.run( args ); }}Copy the code

After this unpacking, we find that we also need to construct the SpringApplication class object first and then call its run() method.

So let’s talk about the construction process of the SpringApplication and the flow of its run() method. Once we understand this, we can see how the Spring Boot application works.

Initialization of the SpringApplication instance

Let’s look at the code:

The four key steps are illustrated below:

  • 1.Infer the type of application: The REACTIVE application, SERVLET application, or NONE is created

2.useSpringFactoriesLoaderFind and loadClasspath under the meta-inf/spring. FactoriesAll available in the fileApplicationContextInitializer

3.useSpringFactoriesLoaderFind and loadClasspath under the meta-inf/spring. FactoriesAll available in the fileApplicationListener

(4)Infer and setmainMethod definition class

SpringApplication’s Run () method

Let’s look at what the code looks like:

We summarize and refine each step as follows:

  1. Through SpringFactoriesLoader loading meta-inf/spring. Factories file, acquire and create SpringApplicationRunListener object

  2. Then starting messages sent by SpringApplicationRunListener

  3. Create parameters and configure the Environment to be used by the current SpringBoot application

  4. Completed, still issued by SpringApplicationRunListener environmentPrepared news

  5. Create ApplicationContext

  6. Initialize the ApplicationContext, set the Environment, load the configuration, and so on

  7. ContextPrepared messages sent by SpringApplicationRunListener, told Spring Boot applications use the ApplicationContext ready to OK

  8. Loads of various beans into ApplicationContext, continue contextLoaded messages sent by SpringApplicationRunListener, Tell Spring Boot that the ApplicationContext used by the application is loaded OK

  9. Refresh ApplicationContext completes the final step of making the IoC container available

  10. Issued by SpringApplicationRunListener started

  11. Call callRunners (…). Method to execute the Run method that implements the ApplicationRunner and CommandLineRunner interface classes to perform additional operations once the Spring application context is ready. Thus complete the final program start.

  12. Running messages sent by SpringApplicationRunListener, told has to run the program

At this point, the whole process is over.

Biography: Swing unruly, love life.Java Cultivator (wechat official ID: Java Cultivator)Welcome your attention. You can get the latest Java tutorials I’ve put together, all dry stuff. Like the bottom of the source code can be friends to exchange