Spring boot in-depth principle-springapplication startup principle

Source: Internet
Author: User

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:

    1. Public static void main(String[] args) throws Exception {
    2. springapplication. Run(application. Class, args);
    3. }

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:

  1. Public Configurableapplicationcontext run(String... args) {
  2. StopWatch StopWatch = new StopWatch();
  3. StopWatch. Start();
  4. configurableapplicationcontext Context = null;
  5. failureanalyzers Analyzers = null;
  6. Configureheadlessproperty();
  7. //Initialize listener
  8. springapplicationrunlisteners Listeners = getrunlisteners(args);
  9. //Release applicationstartedevent
  10. Listeners. Starting();
  11. try {
  12. //Assembly parameters and Environment
  13. applicationarguments applicationarguments = new defaultapplicationarguments(
  14. args);
  15. configurableenvironment Environment = prepareenvironment(listeners,
  16. Applicationarguments);
  17. //Print banner
  18. Banner Printedbanner = printbanner(environment);
  19. //Create ApplicationContext ()
  20. Context = createapplicationcontext();
  21. Analyzers = new failureanalyzers(context);
  22. //Assembly Context
  23. Preparecontext(context, environment, listeners, applicationarguments,
  24. Printedbanner);
  25. //refreshcontext
  26. Refreshcontext(context);
  27. //afterrefresh
  28. AfterRefresh(context, applicationarguments);
  29. //Release applicationreadyevent
  30. Listeners. Finished(context, null);
  31. StopWatch. Stop();
  32. if (this. Logstartupinfo) {
  33. new startupinfologger(this. Mainapplicationclass)
  34. . logstarted(getapplicationlog(), stopWatch);
  35. }
  36. return context;
  37. }
  38. catch (throwable ex) {
  39. Handlerunfailure(context, listeners, analyzers, ex);
  40. throw new illegalstateexception(ex);
  41. }
  42. }
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>

    1. Public Interface Applicationlistener<E extends applicationevent > Extends eventlistener

For example, if you want to listen to applicationstartedevent events, you can define:

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.