应用程序事件在您的应用程序运行时按以下顺序发送:
- ApplicationStartingEvent 创建完成SpringApplication对象后执行SpringApplicationRunListeners.starting,发送一个ApplicationStartingEvent 事件到广播器SimpleApplicationEventMulticaster,SimpleApplicationEventMulticaster将遍历所有的listeners,触发所有支持ApplicationStartingEvent 事件的监听器。
EventPublishingRunListener:
public void starting() {
this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
}
- ApplicationEnvironmentPreparedEvent, Environment配置完成后,发送一个ApplicationEnvironmentPreparedEvent事件到广播器SimpleApplicationEventMulticaster,触发所有支持ApplicationEnvironmentPreparedEvent事件的监听器。ApplicationContextInitializedEvent
EventPublishingRunListener:
public void environmentPrepared(ConfigurableEnvironment environment) {
this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));
}
- ApplicationContextInitializedEvent,在ApplicationContext准备好且已调用ApplicationContextInitializers之后但未加载任何bean定义之前,发送ApplicationContextInitializedEvent事件到广播器SimpleApplicationEventMulticaster,触发所有支持ApplicationContextInitializedEvent事件的监听器。
public void contextPrepared(ConfigurableApplicationContext context) {
this.initialMulticaster.multicastEvent(new ApplicationContextInitializedEvent(this.application, this.args, context));
}
- ApplicationPreparedEvent,在刷新开始之前但在加载系统和主函数bean定义之后,bean class loader 创建之后发送一个ApplicationPreparedEvent事件到广播器SimpleApplicationEventMulticaster,触发所有支持ApplicationPreparedEvent事件的监听器。
public void contextLoaded(ConfigurableApplicationContext context) {
ApplicationListener listener;
for(Iterator var2 = this.application.getListeners().iterator(); var2.hasNext(); context.addApplicationListener(listener)) {
listener = (ApplicationListener)var2.next();
if (listener instanceof ApplicationContextAware) {
((ApplicationContextAware)listener).setApplicationContext(context);
}
}
this.initialMulticaster.multicastEvent(new ApplicationPreparedEvent(this.application, this.args, context));
}
- ApplicationStartedEvent上下文已被刷新后,但是任何应用程序和Runner被调用前,发送一个ApplicationStartedEvent事件到广播器SimpleApplicationEventMulticaster,触发所有支持ApplicationStartedEvent事件的监听器,紧接着
AvailabilityChangeEvent其后发送的LivenessState.CORRECT,表示该应用程序被视为已生效。
public void started(ConfigurableApplicationContext context) {
context.publishEvent(new ApplicationStartedEvent(this.application, this.args, context));
AvailabilityChangeEvent.publish(context, LivenessState.CORRECT);
}
- ApplicationReadyEvent, Runner被调用后发送一个ApplicationReadyEvent事件到广播器SimpleApplicationEventMulticaster,触发所有支持ApplicationReadyEvent事件的监听器,。紧随AvailabilityChangeEvent其后发送,ReadinessState.ACCEPTING_TRAFFIC以指示该应用程序已准备好处理请求。
public void running(ConfigurableApplicationContext context) {
context.publishEvent(new ApplicationReadyEvent(this.application, this.args, context));
AvailabilityChangeEvent.publish(context, ReadinessState.ACCEPTING_TRAFFIC);
}
- ApplicationFailedEvent,在启动时异常发送ApplicationFailedEvent事件到广播器SimpleApplicationEventMulticaster,触发所有支持ApplicationFailedEvent事件的监听器
public void failed(ConfigurableApplicationContext context, Throwable exception) {
ApplicationFailedEvent event = new ApplicationFailedEvent(this.application, this.args, context, exception);
if (context != null && context.isActive()) {
context.publishEvent(event);
} else {
if (context instanceof AbstractApplicationContext) {
Iterator var4 = ((AbstractApplicationContext)context).getApplicationListeners().iterator();
while(var4.hasNext()) {
ApplicationListener<?> listener = (ApplicationListener)var4.next();
this.initialMulticaster.addApplicationListener(listener);
}
}
this.initialMulticaster.setErrorHandler(new EventPublishingRunListener.LoggingErrorHandler());
this.initialMulticaster.multicastEvent(event);
}
}
上面的列表仅包含SpringApplicationEvent与绑定的SpringApplication。除这些以外,以下事件也在ApplicationPreparedEvent之后和ApplicationStartedEvent之前发布:
- web server准备就绪后发送一个WebServerInitializedEvent事件,此事件是在org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.start()方法中启动server后调用AbstractApplicationContext.publishEvent发布事件。
org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle
class WebServerStartStopLifecycle implements SmartLifecycle {
private final ServletWebServerApplicationContext applicationContext;
private final WebServer webServer;
private volatile boolean running;
WebServerStartStopLifecycle(ServletWebServerApplicationContext applicationContext, WebServer webServer) {
this.applicationContext = applicationContext;
this.webServer = webServer;
}
public void start() {
this.webServer.start();
this.running = true;
this.applicationContext.publishEvent(new ServletWebServerInitializedEvent(this.webServer, this.applicationContext));
}
public void stop() {
this.webServer.stop();
}
public boolean isRunning() {
return this.running;
}
public int getPhase() {
return 2147483646;
}
}
- ReactiveWebServerInitializedEvent是servlet的反应式编程,此事件是在org.springframework.boot.web.reactive.context.WebServerStartStopLifecycle.start里发出的。
org.springframework.boot.web.reactive.context.WebServerStartStopLifecycle:
class WebServerStartStopLifecycle implements SmartLifecycle {
private final WebServerManager weServerManager;
private volatile boolean running;
WebServerStartStopLifecycle(WebServerManager weServerManager) {
this.weServerManager = weServerManager;
}
public void start() {
this.weServerManager.start();
this.running = true;
}
public void stop() {
this.running = false;
this.weServerManager.stop();
}
public boolean isRunning() {
return this.running;
}
public int getPhase() {
return 2147483646;
}
}
org.springframework.boot.web.reactive.context.WebServerManager
class WebServerManager {
private final ReactiveWebServerApplicationContext applicationContext;
private final WebServerManager.DelayedInitializationHttpHandler handler;
private final WebServer webServer;
WebServerManager(ReactiveWebServerApplicationContext applicationContext, ReactiveWebServerFactory factory, Supplier<HttpHandler> handlerSupplier, boolean lazyInit) {
this.applicationContext = applicationContext;
Assert.notNull(factory, "Factory must not be null");
this.handler = new WebServerManager.DelayedInitializationHttpHandler(handlerSupplier, lazyInit);
this.webServer = factory.getWebServer(this.handler);
}
void start() {
this.handler.initializeHandler();
this.webServer.start();
this.applicationContext.publishEvent(new ReactiveWebServerInitializedEvent(this.webServer, this.applicationContext));
}
void shutDownGracefully(Runnable callback) {
this.webServer.shutDownGracefully((result) -> {
callback.run();
});
}
void stop() {
this.webServer.stop();
}
WebServer getWebServer() {
return this.webServer;
}
HttpHandler getHandler() {
return this.handler;
}
......
}