Spring boot in-depth principle-springapplication startup principle
We know that if you don't need a special configuration, just call the Springapplicatio.run () method in the main method to start the Spring boot app:
- Public static void main(String[] args) throws Exception {
- springapplication. Run(application. Class, args);
- }
As the first in-depth principle, let's take a look at how the spring boot application started.
Springapplication START Process
The boot process for springapplication is very complex, and here are the key actions that start after calling the Springapplication.run method:
Since you want to understand the springapplication start-up process, the first step of course is to enter the source code to see:
- Public Configurableapplicationcontext run(String... args) {
- StopWatch StopWatch = new StopWatch();
- StopWatch. Start();
- configurableapplicationcontext Context = null;
- failureanalyzers Analyzers = null;
- Configureheadlessproperty();
- //Initialize listener
- springapplicationrunlisteners Listeners = getrunlisteners(args);
- //Release applicationstartedevent
- Listeners. Starting();
- try {
- //Assembly parameters and Environment
- applicationarguments applicationarguments = new defaultapplicationarguments(
- args);
- configurableenvironment Environment = prepareenvironment(listeners,
- Applicationarguments);
- //Print banner
- Banner Printedbanner = printbanner(environment);
- //Create ApplicationContext ()
- Context = createapplicationcontext();
- Analyzers = new failureanalyzers(context);
- //Assembly Context
- Preparecontext(context, environment, listeners, applicationarguments,
- Printedbanner);
- //refreshcontext
- Refreshcontext(context);
- //afterrefresh
- AfterRefresh(context, applicationarguments);
- //Release applicationreadyevent
- Listeners. Finished(context, null);
- StopWatch. Stop();
- if (this. Logstartupinfo) {
- new startupinfologger(this. Mainapplicationclass)
- . logstarted(getapplicationlog(), stopWatch);
- }
- return context;
- }
- catch (throwable ex) {
- Handlerunfailure(context, listeners, analyzers, ex);
- throw new illegalstateexception(ex);
- }
- }
The first step is to initialize the listener
This will initialize the spring boot listener and the custom listener added to the springapplication.
Initialization listener call relationship is very deep, in order to save space, do not paste the source, and later with a special article to chat.
Step two, release the Applicationstartedevent event
At this step, Spring Boot publishes a applicationstartedevent event. If you want to execute some code at this time can be implemented by implementing the Applicationlistener interface;
Here is the definition of the Applicationlistener interface, note there is a <e extends applicationevent>
- Public Interface Applicationlistener<E extends applicationevent > Extends eventlistener
For example, if you want to listen to applicationstartedevent events, you can define:
- Public class Applicationstartedlistener implements Applicationlistener <applicationstartedevent>
And then through Springapplication.addlistener (..) Add in.
Step three, assembly parameters and environment
In this step, the parameters are initialized first, and then the environment is assembled to determine whether it is a web environment or a non-web environment.
Fourth step, release Applicationenvironmentpreparedevent event
Precisely, this should belong to the third step, after assembling the environment, it triggers the Applicationenvironmentpreparedevent event. If you want to execute some code at this time, you can subscribe to the listener of this event, the same way as the second step.
Fifth Step, print banner
See the Spring Boot instance Tutorial-custom banner The alumni are familiar with, and the starting banner is printed out in this step.
Sixth step, create ApplicationContext
This will be based on whether it is a web environment, to decide what type of applicationcontext,applicationcontext to create do not say more, do not know what ApplicationContext is the classmate, Go left and fill in the basics of spring.
Seventh step, assemble the context
Context variables, registration initializers, Beannamegenerator, etc. are set up here.
Eighth step, Release Applicationpreparedevent event
This is more accurate in the seventh step because it was released when the context was assembled.
Notable is: Here is False, False, false, the source is empty, and did not really release the Applicationpreparedevent event. Do not know the author thinks so???
Nineth step, registration, loading, etc.
Register springapplicationarguments, Springbootbanner, load resources, and more.
Tenth step, release Applicationpreparedevent event
Note that the Applicationpreparedevent event is actually released here. This and the eighth step are misleading.
11th Step, Refreshcontext
Assemble a very important core component such as the context beanfactory.
12th Step, Afterrefreshcontext
This will call the custom runners and don't know what runners is, please refer to the Spring Boot official documentation-Springapplication
13th step, Release Applicationreadyevent event
The final step, the release of the Applicationreadyevent event, started, indicating that the service is ready to start the normal provision of services. Usually we listen to this event to print some log of the nature of the monitoring, indicating that the application started normally. Add the method with the second step.
Note: If startup fails, this step publishes the Applicationfailedevent event.
Here, some of the key actions started by Spring boot are finished.
Summarize
Said to be in-depth, this article is more like a general introduction. Because each key action content is a lot of, it is worth in-depth study, all placed here obviously space is too small, next will be divided into chapters into each key action.
Spring boot in-depth principle-springapplication startup principle