Spring执行顺序:Event 事件
实现 ApplicationListener 接口,输出所有事件。
当以 @Component 方式配置时
事件触发顺序如下:
- ApplicationListener#ContextRefreshedEvent
- ApplicationListener#ServletWebServerInitializedEvent
- ApplicationListener#ApplicationStartedEvent
- ApplicationListener#ApplicationReadyEvent
- ApplicationListener#ContextClosedEvent
当通过 /META-INF/spring.factories 配置时
配置内容如下:
1 | org.springframework.context.ApplicationListener=com.github.abel533.event.ApplicationListenerImpl |
此时输出的事件顺序如下:
ApplicationListener#ApplicationStartingEvent
ApplicationListener#ApplicationEnvironmentPreparedEvent
ApplicationListener#ApplicationContextInitializedEvent
ApplicationListener#ApplicationPreparedEvent
差异
很容易通过对比发现,Event 触发的时间极早,以至于 @Component 方式只能从第 4 个事件才开始获取到。从这两种方式的加载时机来看这个差异产生的原因。
在 SpringApplication 构造方法中,就调用 getSpringFactoriesInstances 来获取 /META-INF/spring.factories 配置的 ApplicationListener,代码如下:
1 | public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) { |
在 SpringFactoriesLoader#loadFactoryNames 实现了从该配置文件获取实现名的方法。从这之后就能收到后续触发的事件。
通过 @Component 方式时,在 SpringApplication#refresh 中调用 registerListeners 获取的所有 ApplicationListener 接口的实现。代码如下:
1 |
|
下面先分析前 4 个无法获取的事件顺序。
ApplicationStartingEvent
第 0 个事件是在 EventPublishingRunListener#starting 中发布的,代码如下:
1 | @Override |
此时的堆栈调用情况如下:
onApplicationEvent:15, ApplicationListenerImpl (com.github.abel533.event)
doInvokeListener:172, SimpleApplicationEventMulticaster (org.springframework.context.event)
invokeListener:165, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:139, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:127, SimpleApplicationEventMulticaster (org.springframework.context.event)
starting:69, EventPublishingRunListener (org.springframework.boot.context.event)
starting:48, SpringApplicationRunListeners (org.springframework.boot)
run:302, SpringApplication (org.springframework.boot)
run:1260, SpringApplication (org.springframework.boot)
run:1248, SpringApplication (org.springframework.boot)
main:12, ListenerApplication (com.github.abel533.event)
ApplicationEnvironmentPreparedEvent
第 1 个事件是在 EventPublishingRunListener#environmentPrepared 中发布的,代码如下:
1 | @Override |
此时的堆栈调用情况如下:
onApplicationEvent:15, ApplicationListenerImpl (com.github.abel533.event)
doInvokeListener:172, SimpleApplicationEventMulticaster (org.springframework.context.event)
invokeListener:165, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:139, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:127, SimpleApplicationEventMulticaster (org.springframework.context.event)
environmentPrepared:75, EventPublishingRunListener (org.springframework.boot.context.event)
environmentPrepared:54, SpringApplicationRunListeners (org.springframework.boot)
prepareEnvironment:347, SpringApplication (org.springframework.boot)
run:306, SpringApplication (org.springframework.boot)
run:1260, SpringApplication (org.springframework.boot)
run:1248, SpringApplication (org.springframework.boot)
main:12, ListenerApplication (com.github.abel533.event)
ApplicationContextInitializedEvent
第 2 个事件是在 EventPublishingRunListener#contextPrepared 中发布的,代码如下:
1 | @Override |
此时的堆栈调用情况如下:
onApplicationEvent:15, ApplicationListenerImpl (com.github.abel533.event)
doInvokeListener:172, SimpleApplicationEventMulticaster (org.springframework.context.event)
invokeListener:165, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:139, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:127, SimpleApplicationEventMulticaster (org.springframework.context.event)
contextPrepared:81, EventPublishingRunListener (org.springframework.boot.context.event)
contextPrepared:60, SpringApplicationRunListeners (org.springframework.boot)
prepareContext:374, SpringApplication (org.springframework.boot)
run:314, SpringApplication (org.springframework.boot)
run:1260, SpringApplication (org.springframework.boot)
run:1248, SpringApplication (org.springframework.boot)
main:12, ListenerApplication (com.github.abel533.event)
ApplicationPreparedEvent
第 3 个事件是在 EventPublishingRunListener#contextLoaded 中发布的,代码如下:
1 | @Override |
此时的堆栈调用情况如下:
onApplicationEvent:15, ApplicationListenerImpl (com.github.abel533.event)
doInvokeListener:172, SimpleApplicationEventMulticaster (org.springframework.context.event)
invokeListener:165, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:139, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:127, SimpleApplicationEventMulticaster (org.springframework.context.event)
contextLoaded:93, EventPublishingRunListener (org.springframework.boot.context.event)
contextLoaded:66, SpringApplicationRunListeners (org.springframework.boot)
prepareContext:393, SpringApplication (org.springframework.boot)
run:314, SpringApplication (org.springframework.boot)
run:1260, SpringApplication (org.springframework.boot)
run:1248, SpringApplication (org.springframework.boot)
main:12, ListenerApplication (com.github.abel533.event)
ContextRefreshedEvent
在上面差异中提到 finishRefresh 会触发 ContextRefreshedEvent,代码如下:
1 | @Override |
注意 super.finishRefresh,代码如下(有删减):
1 | protected void finishRefresh() { |
ServletWebServerInitializedEvent
注意前面 finishRefresh 方法,如果存在 webServer != null,就会发布 ServletWebServerInitializedEvent。
ApplicationStartedEvent
在 SpringApplication#run 方法中,执行完成后,就会调用 listeners.started(context); 方法,在这里面会发布 ApplicationStartedEvent。
ApplicationReadyEvent
和上面 ApplicationStartedEvent 一样,如下代码(有删减):
1 | // ApplicationStartedEvent |
执行完所有 ApplicationRunner 和 CommandLineRunner 接口方法后,就会调用 listeners.running(context),在这里面就会发布 ApplicationReadyEvent。
在这之后就没有运行期的主要事件了(不考虑 devtools 重启)。
ContextClosedEvent
当调用关闭方法的时候,自然就触发了 ContextClosedEvent,调用堆栈如下:
onApplicationEvent:20, ApplicationListenerImpl (com.github.abel533.event)
doInvokeListener:172, SimpleApplicationEventMulticaster (org.springframework.context.event)
invokeListener:165, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:139, SimpleApplicationEventMulticaster (org.springframework.context.event)
publishEvent:398, AbstractApplicationContext (org.springframework.context.support)
publishEvent:355, AbstractApplicationContext (org.springframework.context.support)
doClose:994, AbstractApplicationContext (org.springframework.context.support)
close:961, AbstractApplicationContext (org.springframework.context.support)
main:12, ListenerApplication (com.github.abel533.event)