SpringBoot execution process
Basic overview
SpringBoot
The Spring
application 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. SpringBoot
The 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
SpringApplicationRunListener
It is SpringBoot
a 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 SpringBoot
default, only one is implemented org.springframework.boot.context.event.EventPublishingRunListener
. Through this class, SpringBoot
different application event types are published at different points in time at startup ApplicationEvent
. SpringBoot
Loaded at initialization ApplicationListener
If 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 ApplicationContext
calling 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. SpringBoot
Provides us with two interfaces to help us achieve this requirement. The two interfaces are CommandLineRunner
and ApplicationRunner
. Their execution time is when the container is started and completed.
There is one run
method in these two interfaces, we only need to implement this method. The difference between the two interfaces is that the parameters of ApplicationRunner
the methods in the interface are , while the parameters of the methods in the interface are arrays.run
ApplicationArguments
CommandLineRunner
run
String
If there are multiple implementation classes and you need them to be executed in a certain order, you can add @Order
annotations to the implementation classes. @Order(value=整数值)
. SpringBoot
It will be executed in order from small to large according @Order
to 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