Moment For Technology

The SpringBoot application startup process

Posted on Dec. 3, 2022, 9:22 a.m. by Michelle Love
Category: The back-end Tag: The back-end spring The container

This article contains 946 words and takes about 3 minutes to read!

An overview of the

There are two key elements that strike me as the first to come across SpringBoot:

Compared to the typical code above, the two elements are:

  • @SpringBootApplication
  • SpringApplication and the run() method

The @SpringBootApplication annotation has been dissected above: "SpringBoot @SpringBootApplication annotation behind the three-body structure probe" completed, in fact, it is behind a three-body structure, but SpringBoot gave it a package. So in this article, let's take a look at the SpringApplication and the run() method, and what's behind it?

Note: This article first appeared on My Personal Blog, welcome to the website

The brain map of this article is as follows:

A glimpse of SpringApplication

The SpringApplication class is an innovation of the SpringBoot framework. It does not exist in the original Spring. SpringApplication encapsulates a set of Spring application startup processes, but it is completely transparent to the user. So when we started, SpringBoot felt simple and lightweight.

Generally speaking, the default SpringApplication execution process already meets most of the requirements, but if the user wants to intervene in the process, the process can be extended through extension points opened by the SpringApplication somewhere in the process. The typical extension solution is to use the set method.

Let's take an example of a common SpringBoot application boot class and break it down:

@SpringBootApplication public class CodeSheepApplication { public static void main( String[] args ) { // CodeSheepApplication.class args ); / / this is a traditional SpringBoot application startup, one line of code, internal default did many things SpringApplication app = new SpringApplication (CodeSheepApplication. Class); app.setXXX( ... ) ; // The extension is here!! args ); }}Copy the code

After this unpacking, we find that we also need to construct the SpringApplication class object and then call the run() method of that object. So let's talk about the construction process of SpringApplication and the process of its run() method. Once you understand this, you will understand how SpringBoot application works!

Initialization of the SpringApplication instance

Let's look at the code:

The four key steps have been highlighted in the figure and are explained as follows:

  • (1) Infer the application type: The application to be created is REACTIVE, SERVLET, or NONE

  • 2.useSpringFactoriesLoaderFind and load the classpathMETA-INF/spring.factoriesAll available in the fileApplicationContextInitializer

  • 3.useSpringFactoriesLoaderFind and load the classpathMETA-INF/spring.factoriesAll available in the fileApplicationListener

  • (4) Infer and set the definition class of main

Explore the run() method of the SpringApplication

Here's what the code looks like:

I have marked the main steps in the figure above. In addition, I have drawn a flowchart according to my own understanding, as shown below, which can be compared with the numbers.

We summarize 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 ApplicationContext, set Environment, load related configuration, etc

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

  8. Loads of various beans into ApplicationContext, continue contextLoaded messages sent by SpringApplicationRunListener, Notifies SpringBoot that the ApplicationContext used by the application has been loaded with OK

  9. Refresh ApplicationContext, which completes the last step in making the IoC container available

  10. Issued by SpringApplicationRunListener started

  11. Complete the final program initiation

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

At this point, the whole process is over!


Due to the limited ability, if there is any mistake or improper place, please criticize and correct, learn and communicate together!

Long press or scan the following careful heart to subscribe to the author of the public account CodeSheep, get more pragmatic, can understand, can reproduce the original ↓↓

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.