接上文Spring中refresh分析之initMessageSource方法详解我们分析过initMessageSource后,本文分析initApplicationEventMulticaster方法。
方法如下所示,初始化ApplicationEventMulticaster。如果应用上下文环境中没有定义,则使用SimpleApplicationEventMulticaster 。
protected void initApplicationEventMulticaster() { ConfigurableListableBeanFactory beanFactory = getBeanFactory(); // 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 + "]"); } } 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() + "]"); } } }
首先尝试从容器中获取applicationEventMulticaster,如果得不到则实例化SimpleApplicationEventMulticaster并赋予this.applicationEventMulticaster。这里其指的是AbstractApplicationContext的成员applicationEventMulticaster。
实例化SimpleApplicationEventMulticaster后,会调用registerSingleton方法进行注册。我们这里再回顾下DefaultListableBeanFactory的registerSingleton方法流程逻辑。
如下所示其首先调用父类DefaultSingletonBeanRegistry的registerSingleton方法然后更新manualSingletonNames并清空缓存。
// DefaultListableBeanFactory#registerSingleton @Override public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException { //调用父类 super.registerSingleton(beanName, singletonObject); // 更新到manualSingletonNames中 updateManualSingletonNames(set -> set.add(beanName), set -> !this.beanDefinitionMap.containsKey(beanName)); //清空allBeanNamesByType、singletonBeanNamesByType clearByTypeCache(); }
这里manualSingletonNames是volatile Set manualSingletonNames,维护了手动注册的单例beanName。
我们继续看父类DefaultSingletonBeanRegistry的registerSingleton方法。
// DefaultSingletonBeanRegistry#registerSingleton @Override public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException { Assert.notNull(beanName, "Bean name must not be null"); Assert.notNull(singletonObject, "Singleton object must not be null"); synchronized (this.singletonObjects) { Object oldObject = this.singletonObjects.get(beanName); if (oldObject != null) { throw new IllegalStateException("Could not register object [" + singletonObject + "] under bean name '" + beanName + "': there is already object [" + oldObject + "] bound"); } addSingleton(beanName, singletonObject); } }
其首先会从singletonObjects(一级缓存)中根据beanName获取对象,如果不为null则抛出异常,否则调用addSingleton方法。该方法如下所示其将会添加到一级缓存,从三级缓存、二级缓存移除,beanName添加到registeredSingletons中。
//DefaultSingletonBeanRegistry#addSingleton protected void addSingleton(String beanName, Object singletonObject) { synchronized (this.singletonObjects) { this.singletonObjects.put(beanName, singletonObject); this.singletonFactories.remove(beanName); this.earlySingletonObjects.remove(beanName); this.registeredSingletons.add(beanName); } }