SpringBoot-startup configuration principle

1. Startup process:

Several important event callback mechanisms

The following two are configured in META-INF/spring.factories:

  • ApplicationContextInitializer
  • SpringApplicationRunListener

The following two are configured in the ioc container

  • ApplicationRunner
  • CommandLineRunner

1. Create SpringApplication:

Starting a SpringBoot application will first create a SpringApplication, and then call the run method.
Insert picture description here
new SpringApplication (main program class)

  • Determine whether web application
  • Load and save all ApplicationContextInitializer (META-INF/spring.factories),
  • Load and save all ApplicationListener
  • Get the main program class

Creating SpringApplication is actually calling the constructor of SpringApplication, and completing the application type judgment and loading of Initializers, Listeners, etc. in it.
Insert picture description here
(1) webApplicationType: returns the type of the current web application.
None: non-web application starts
SERVLET: servlet-based web application and uses embedded server
REACTIVE: non-blocking
Insert picture description here

(2) Initializer and listener loading
setInitializers : load the initializer
setListenersfrom META-INF/spring.factories: load the listener from META-INF/spring.factories
Insert picture description here
Many packages have spring.factories, the following is an example of the autoconfiguration package:
Insert picture description here
After startup, the initializer and listener successfully loaded
Insert picture description here
(3) deduceMainApplicationClass()
Insert picture description here

Look at the passed class, which class has a main method, and which class is the main program.
Insert picture description here

2、run

run()

  • Call back all the starting of SpringApplicationRunListener (META-INF/spring.factories)
  • Get ApplicationArguments
  • Prepare environment & callback environmentPrepared of all listeners (SpringApplicationRunListener)
  • Print banner information
  • Create ioc container object (web environment container or ordinary environment container)
  • Prepare the environment
  • 执行ApplicationContextInitializer. initialize()
  • Listener SpringApplicationRunListener callback contextPrepared
  • Load main configuration class definition information
  • Listener SpringApplicationRunListener callback contextLoaded
  • Refresh and start the IOC container;
  • Scan and load components in all containers (including all EnableAutoConfiguration components obtained from META-INF/spring.factories)
  • Call back all the run methods of ApplicationRunner and CommandLineRunner in the container
  • Listener SpringApplicationRunListener callback started method and running method

The complete flow of the run method:
Insert picture description here
Insert picture description here

2. Event monitoring mechanism test

The following two need to be configured in META-INF/spring.factories:

  • ApplicationContextInitializer
  • SpringApplicationRunListener

Insert picture description here

The following two configurations can be injected into the ioc container

  • ApplicationRunner
  • CommandLineRunner
    Insert picture description here

Guess you like

Origin blog.csdn.net/glpghz/article/details/111424694