初始化事件监听多路广播器
初始化事件监听多路广播器
/**
* 初始化事件监听多路广播器
*/
initApplicationEventMulticaster();
注册SimpleApplicationEventMulticaster
内容非常简单,先判断有没有自定义的ApplicationEventMulticaster,没有的话就注册一个。
SimpleApplicationEventMulticaster就是用来发布事件用的。具体可看另一篇文章Spring Framework之监听器的使用,其中有讲到如何发布事件以及发布事件的源码跟踪
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
/**
* 判断容器中是否存在bdName为applicationEventMulticaster的bd
* 也就是自定义的事件监听多路广播器,必须实现ApplicationEventMulticaster接口
*/
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isDebugEnabled()) {
logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
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 + "]");
}
}
}