初始化事件多波器
AbstractApplicationContext.refresh()—>initApplicationEventMulticaster();–>被观察者
代码展示解析
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//判断IOC容器中包含applicationEventMultiaster事件多波器的Bean的name
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
//创建一个applicationEventMultiaster的bean放在IOC容器中,Bean的name为applicationEventMultiaster
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isDebugEnabled()) {
logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
//容器中不包含beanName为applicationEventMultiaster的多波器组件
else {
//创建一个SimpleApplicationEventMulticaster 多播器
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
//注册到容器中
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
"': using default [" + this.applicationEventMulticaster + "]");
}
}
}
把容器中的监听器注册到多波器上去
AbstractApplicationContext.refresh()—>registerListeners();–>观察者
代码展示解析
//检查侦听器bean并注册它们
protected void registerListeners() {
// Register statically specified listeners first.
//去容器中把applicationListener捞取出来注册到多波器上
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<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
如何发布事件
AbstractApplicationContext.refresh()—>registerListeners()—>multicastEvent();
代码展示及解析
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
//获取到所有的监听器-->getApplicationListeners
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
//看spring容器中是否支持线程池,异步发送事件
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(new Runnable() {
@Override
public void run() {
invokeListener(listener, event);
}
});
}
else {//同步发送事件
invokeListener(listener, event);
}
}
}
后续还会更新