Spring Boot execution process

SpringBoot execution process

Basic overview

SpringBootThe Springapplication startup process is "templated", so we can SpringApplication.run(XXX.class, args)start one-stop in a way. Its internal logic is also a relatively complex process, and the execution flow will be explained below. SpringBootThe version referenced in this process is 1.4.3.RELEASE.

A brief schematic diagram of the SpringBoot application startup steps

From the above brief diagram, it can be seen that as long as the extension point of event notification is ignored, the execution process of Spring Boot is not as complicated as imagined.

SpringApplicationRunListener

SpringApplicationRunListenerIt is SpringBoota listener for time notifications at different execution time points during the execution process. Generally speaking, it is not necessary to implement one by yourself SpringApplicationRunListener, even if it is the SpringBootdefault, only one is implemented org.springframework.boot.context.event.EventPublishingRunListener. Through this class, SpringBootdifferent application event types are published at different points in time at startup ApplicationEvent. SpringBootLoaded at initialization ApplicationListenerIf you are interested in these events, you can receive and process them.

public interface SpringApplicationRunListener {
    void started();
    void environmentPrepared(ConfigurableEnvironment environment);
    void contextPrepared(ConfigurableApplicationContext context);
    void contextLoaded(ConfigurableApplicationContext context);
    void finished(ConfigurableApplicationContext context, Throwable exception);
}

ApplicationContextInitializer

Through this class, you can further set or process the object before ApplicationContextcalling the refresh()method .ApplicationContext

public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {
    void initialize(C applicationContext);
}

ApplicationRunner和CommandLineRunner

We may have such a scenario in development. Need to execute something when the container starts. Such as reading configuration files, database connections and the like. SpringBootProvides us with two interfaces to help us achieve this requirement. The two interfaces are CommandLineRunnerand ApplicationRunner. Their execution time is when the container is started and completed.

There is one runmethod in these two interfaces, we only need to implement this method. The difference between the two interfaces is that the parameters of ApplicationRunnerthe methods in the interface are , while the parameters of the methods in the interface are arrays.runApplicationArgumentsCommandLineRunnerrunString

If there are multiple implementation classes and you need them to be executed in a certain order, you can add @Orderannotations to the implementation classes. @Order(value=整数值). SpringBootIt will be executed in order from small to large according @Orderto the value in .value

ApplicationRunner

public interface ApplicationRunner {
    void run(ApplicationArguments args) throws Exception;
}

CommandLineRunner

public interface CommandLineRunner {
    void run(String... args) throws Exception;
}

Detailed version of SpringBoot execution process

Link: https://pan.baidu.com/s/1mj2UiA8 Password: sxmz

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325185682&siteId=291194637