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 ) { // SpringApplication.run( 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!! app.run( 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.use
SpringFactoriesLoader
Find and load the classpathMETA-INF/spring.factories
All available in the fileApplicationContextInitializer
- 3.use
SpringFactoriesLoader
Find and load the classpathMETA-INF/spring.factories
All 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:
-
Through SpringFactoriesLoader loading meta-inf/spring. Factories file, acquire and create SpringApplicationRunListener object
-
Then starting messages sent by SpringApplicationRunListener
-
Create parameters and configure the Environment to be used by the current SpringBoot application
-
Completed, still issued by SpringApplicationRunListener environmentPrepared news
-
Create ApplicationContext
-
Initialize ApplicationContext, set Environment, load related configuration, etc
-
ContextPrepared messages sent by SpringApplicationRunListener, told SpringBoot applications use the ApplicationContext ready to OK
-
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
-
Refresh ApplicationContext, which completes the last step in making the IoC container available
-
Issued by SpringApplicationRunListener started
-
Complete the final program initiation
-
Running messages sent by SpringApplicationRunListener, told has to run the program
At this point, the whole process is over!
Afterword.
Due to the limited ability, if there is any mistake or improper place, please criticize and correct, learn and communicate together!
- My Personal Blog
- My six months as a tech blogger
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 ↓↓