[springboot] principle analysis

4. Principle of Spring Boot

Spring principle [ Spring annotation ], SpringMVC principle, automatic configuration principle , SpringBoot principle

1. SpringBoot startup process

  • Create a SpringApplication

    • Save some info.
    • Determine the type of the current application. ClassUtils. Servlets
    • **bootstrappers****:初始启动引导器(**List<Bootstrapper>**):去spring.factories文件中找** org.springframework.boot.**Bootstrapper**
    • ApplicationContextInitializer;去spring.factories****找 ApplicationContextInitializer
      • `List<ApplicationContextInitializer<?>> initializers
    • Find ApplicationListener; application listener. Go to spring.factories**** to find ApplicationListener
      • `List<ApplicationListener<?>> listeners
  • run SpringApplication

    • StopWatch
    • Record application startup time
    • **Create a bootstrap context (Context environment)** createBootstrapContext()
      • Get all the previous bootstrappers and execute intitialize() one by one to complete the setting of the bootstrapper context
    • Let the current application enter headless mode. java.awt.headless
    • Get all RunListeners ** (running listeners) [in order to facilitate event awareness for all Listeners]**
      • getSpringFactoriesInstances 去spring.factories****找 SpringApplicationRunListener.
    • Traverse the SpringApplicationRunListener to call the starting method;
      • It is equivalent to notifying all interested people that the system is in the process of starting and that the project is starting.
    • Save command line arguments; ApplicationArguments
    • Prepare the environment prepareEnvironment();
      • Return or create a basic environment information object. Standard Servlet Environment
      • Configuration environment information object.
        • Read configuration property values ​​for all configuration sources.
      • Binding environment information
      • The listener calls listener.environmentPrepared(); notifies all listeners that the current environment is ready
    • Create an IOC container (createApplicationContext())
      • Create container based on item type (Servlet),
      • AnnotationConfigServletWebServerApplicationContext is currently created
    • Prepare the basic information of the ApplicationContext IOC container prepareContext()
      • Save environment information
      • The post-processing process of the IOC container.
      • apply initializers; applyInitializers;
        • Iterate over all ApplicationContextInitializers. Call initialize.. To initialize the extension function of the ioc container
        • Traverse all listeners and call contextPrepared. EventPublishRunListenr; notify all listeners ****contextPrepared
      • All listeners call contextLoaded. Notify all listeners contextLoaded;
    • ** Refresh the IOC container. **refreshContext
      • Create all components in the container (Spring annotations)
    • Work after container refresh is complete? after Refresh
    • All listeners call listeners.started ( context); notify all listeners started
    • **Call all runners; **callRunners()
      • Get the ApplicationRunner in the container
      • Get the CommandLineRunner in the container
      • Merge all runners and sort by @Order
      • Traverse all runners. call run method
    • If the above is abnormal,
      • Failed to call Listener
    • Call the running method listeners.running(context) of all listeners ; notify all listeners of running
    • **running if there is a problem. Continue to notify failed. ** call **failed of all Listeners ; ** notify all listeners of failed
public interface Bootstrapper {
    
    

	/**
	 * Initialize the given {@link BootstrapRegistry} with any required registrations.
	 * @param registry the registry to initialize
	 */
	void intitialize(BootstrapRegistry registry);

}

img

img

img

@FunctionalInterface
public interface ApplicationRunner {
    
    

	/**
	 * Callback used to run the bean.
	 * @param args incoming application arguments
	 * @throws Exception on error
	 */
	void run(ApplicationArguments args) throws Exception;

}
@FunctionalInterface
public interface CommandLineRunner {
    
    

	/**
	 * Callback used to run the bean.
	 * @param args incoming main method arguments
	 * @throws Exception on error
	 */
	void run(String... args) throws Exception;

}

2、Application Events and Listeners

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-application-events-and-listeners

ApplicationContextInitializer

ApplicationListener

SpringApplicationRunListener

3、ApplicationRunner 与 CommandLineRunner

Guess you like

Origin blog.csdn.net/weixin_50799082/article/details/129611668