}
当程序正常运行时,就会打印出内容了。
[](()内置事件
====================================================================
Spring提供了5个标准事件,如下:
| 事件类型 | 说明 |
| :-: | :-- |
| ContextRefreshedEvent | 调用ConfigurableApplicationContext中的refresh时(refresh的最后会发布这个事件) |
| ContextStartedEvent | 调用ConfigurableApplicationContext中的start时被触发 |
| ContextStoppedEvent | 调用ConfigurableApplicationContext中的stop时被触发 |
| ContextClosedEvent | 调用ConfigurableApplicationContext中的close时被触发 |
| RequestHandledEvent | 在web应用中,当一个请求结束时触发该事件 |
[](()事件处理机制
======================================================================
[](()初始化广播者
首先是初始化广播者,初始化的逻辑是在这个方法中的
org.springframework.context.support.AbstractApplicationContext#initApplicationEventMulticaster
具体源码如下:
/**
-
Initialize the ApplicationEventMulticaster.
-
Uses SimpleApplicationEventMulticaster if none defined in the context.
-
@see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
protected void initApplicat 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ionEventMulticaster() {
//先获取到beanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//看是否有自定义的广播者,注意,广播者的名字必须是APPLICATION_EVENT_MULTICASTER_BEAN_NAME
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace(“Using ApplicationEventMulticaster [” + this.applicationEventMulticaster + “]”);
}
}
else {
//如果没有,那就创建默认的广播者
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace(“No '” + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "’ bean, using " +
“[” + this.applicationEventMulticaster.getClass().getSimpleName() + “]”);
}
}
}
[](()注册收集到的监听器
org.springframework.context.support.AbstractApplicationContext#registerListeners
/**
-
Add beans that implement ApplicationListener as listeners.
-
Doesn’t affect other listeners, which can be added without being beans.
*/
protected void registerListeners() {
// Register statically specified listeners first.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
//注意这里体现了所有的监听器都必须是ApplicationListener类型的
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster…
Set earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
[](()发布事件
我们从
org.springframework.context.support.AbstractApplicationContext#publishEvent(org.springframework.context.ApplicationEvent)
一路点下来,最后真正执行发布逻辑的方法是multicastEvent
,源码如下:
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
Executor executor = getTaskExecutor();
//这个getApplicationListeners是从缓存中以event的sourceType和eventType为key获取到关心这个event的所有的监听者
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
//这里其实就是循环调用了
invokeListener(listener, event);
}
}
}
getApplicationListeners
部分源码:
invokeListener
最后会调用到doInvokeListener
,doInvokeListener
部分源码: