initApplicationEventMulticaster()
/**
* 初始化应用事件广播组,如果没有默认定义则使用SimpleApplicationEventMulticaster
* Initialize the ApplicationEventMulticaster.
* Uses SimpleApplicationEventMulticaster if none defined in the context.
* @see org.springframework.context.event.SimpleApplicationEventMulticaster
*/
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
// 如果BeanFactory含有applicationEventMulticaster类型的bean,
// 则实例化一个ApplicationEventMulticaster作为上下文的成员变量
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 + "]");
}
}
// 如果BeanFactory不含有applicationEventMulticaster类型的bean,
// 则实例化一个SimpleApplicationEventMulticaster作为上下文的成员变量
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() + "]");
}
}
}
spring中有事件,事件广播器,事件监听器等组成事件体系,在initApplicationEventMulticaster方法中对事件广播器做初始化,如果找不到此Bean的配置,则创建一个SimpleApplicationEventMulticaster实例作为事件广播器的bean,并且保存为applicationContext的成员变量.
同样会以"applicationEventMulticaster"为key,以SimpleApplicationEventMulticaster实例为value保存在DefauliSingletonBeanRegistry的 singletonObjects 这个map中