Spring源码情操陶冶-AbstractApplicationContext#registerListeners
承接前文Spring源码情操陶冶-AbstractApplicationContext#onRefresh
约定web.xml
配置的contextClass
为默认值XmlWebApplicationContext
直接源码AbstractRefreshableWebApplicationContext#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!
//从DefaultListableFactory中获取ApplicationListener beans
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
//放入前文创建的SimpleApplicationEventMultlcaster中
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
//设置earlyApplicationEvents集合为空,让finishRefresh()可执行剩下的ApplicationEvent
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
注册ApplicationListener beans到广播集合中,并执行earlyApplicationEvents中的事件,一般为空
下节预告
Spring源码情操陶冶-AbstractApplicationContext#finishBeanFactoryInitialization
作者:南柯问天
出处:http://www.cnblogs.com/question-sky/
本文版权归本人和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。