Spring源码解析-容器功能扩展

时间:2021-10-03 17:17:40

spring中不仅有BeanFactory接口,还提供了一个接口ApplicationContext,用于拓展BeanFactory。

ApplicationContext context = new ClassPathXmlApplicationContext("beanFactoryTest.xml");

那么我们来看一下这个ClassPathXmlApplicationContext

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
this(new String[]{configLocation}, true, (ApplicationContext)null);
}

接着进入这个重载的构造方法

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
super(parent);
//这是对配置文件路径的一个设置
this.setConfigLocations(configLocations);
if(refresh) {
//进行刷新,解析等各种功能实现都在里面实现。
this.refresh();
}

}

进入refresh方法

public void refresh() throws BeansException, IllegalStateException {
Object var1 = this.startupShutdownMonitor;
synchronized(this.startupShutdownMonitor) {
//准备刷新上下文环境
this.prepareRefresh();

来看一下这个方法,这个方法是对系统属性或者环境变量进行准备及验证

protected void prepareRefresh() {
this.startupDate = System.currentTimeMillis();
Object var1 = this.activeMonitor;
synchronized(this.activeMonitor) {
this.active = true;
}

if(this.logger.isInfoEnabled()) {
this.logger.info("Refreshing " + this);
}
//这个方法叫初始属性资源,进入这个方法,我们发现这是个空方法,这个方法可以留给子类覆盖实现,增加了spring的可拓展性,
this.initPropertySources();
//验证需要的属性是否已经加载到环境配置中
this.getEnvironment().validateRequiredProperties();
}

接下来就是初始化BeanFactory,对xml文件进行解析读取,里面复用了配置文件的解析及其他功能,对bean进行提取和注入到容器中,当然还拓展了应用。

 ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();

进入该方法

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
//这个是该方法的核心,初始化BeanFactory,进行xml的读取
this.refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
if(this.logger.isDebugEnabled()) {
this.logger.debug("Bean factory for " + this.getDisplayName() + ": " + beanFactory);
}

return beanFactory;
}

进入refreshBeanFactory,这个方法交给了子类AbstractRefreshableApplicationContext实现

protected final void refreshBeanFactory() throws BeansException {
if(this.hasBeanFactory()) {
this.destroyBeans();
this.closeBeanFactory();
}

try {
//创建bean工厂
DefaultListableBeanFactory beanFactory = this.createBeanFactory();
//指定序列化id
beanFactory.setSerializationId(this.getId());
//定制beanFactory
this.customizeBeanFactory(beanFactory);

进入这个方法查看,从这已经开始了beanfactory的拓展,不同版本的spring源码可能不一样

protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
//设置是否覆盖名称相同不同定义的对象
if(this.allowBeanDefinitionOverriding != null) {
beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding.booleanValue());
}
//设置是否允许bean之间存在循环依赖
if(this.allowCircularReferences != null) {
beanFactory.setAllowCircularReferences(this.allowCircularReferences.booleanValue());
}

}

从配置文件中加载bean到bean工厂中

            this.loadBeanDefinitions(beanFactory);

接着看这个方法,这个也是子类方法,看到这是不是有点似曾相识,这是对bean的提取

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
//实例化xml配置文件bean读取器
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
//设置环境变量 beanDefinitionReader.setEnvironment(this.getEnvironment());
//设置加载器
beanDefinitionReader.setResourceLoader(this);
//设置EntityResolver,看过之前文章的我们知道,这个可以帮我们找到约束文件
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
//初始化读取器 this.initBeanDefinitionReader(beanDefinitionReader);
//提取bean
this.loadBeanDefinitions(beanDefinitionReader);
}

看这个方法,主要是对配置文件资源的提取,再进行bean的注册,这个loadBeanDefinitions跟之前beanfactory加载的xml文件是一样的,就不多说了,有兴趣可以看一下之前的博客

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = this.getConfigResources();
if(configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = this.getConfigLocations();
if(configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}

}
            Object var2 = this.beanFactoryMonitor;
synchronized(this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
} catch (IOException var5) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + this.getDisplayName(), var5);
}
}

现在已经将bean注册到了beanfactory中

            //对功能进行填充
this.prepareBeanFactory(beanFactory);

进入这个方法

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
//设置类加载器 beanFactory.setBeanClassLoader(this.getClassLoader());
//设置表达式语言处理器,增加了对SPEL表达式的支持,在完成bean的初始化和属性填充时,来对属性遍历解析,根据语言解析器来解析这个属性,比如#{},最后再进行填充
beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
//注册属性编辑器的注册器,比如Date类型的属性注入需要我们自己完成属性转换,这是我们可以创建一个自定义的属性编辑器,可是这个注册在什么时候注册的一堆属性编辑器呢,在bean初始化,就是创建实例之初,调用了一个initBeanWrapper方法,在这里面调用了registerCustomEditors来注册所有的属性编辑器,在属性填充的环节,让属性编辑器进行属性转换
beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, this.getEnvironment()));
//添加BeanPostProcessor处理器
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
//设置几个忽略自动装配的接口 beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);
beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
beanFactory.registerResolvableDependency(ResourceLoader.class, this);
beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
beanFactory.registerResolvableDependency(ApplicationContext.class, this);
//增加对AspectJ的支持
if(beanFactory.containsBean("loadTimeWeaver")) {
beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
}
//添加默认的系统环境
if(!beanFactory.containsLocalBean("environment")) {
beanFactory.registerSingleton("environment", this.getEnvironment());
}

if(!beanFactory.containsLocalBean("systemProperties")) {
beanFactory.registerSingleton("systemProperties", this.getEnvironment().getSystemProperties());
}

if(!beanFactory.containsLocalBean("systemEnvironment")) {
beanFactory.registerSingleton("systemEnvironment", this.getEnvironment().getSystemEnvironment());
}

}

我们来看一下这个ApplicationContextAwareProcessor处理器,我们还记得在创建bean后,会有一个激活bean的init-method方法,在这前后又会调用初始化前置处理方法和初始化后置处理方法,postProcessBeforeInitialization先来看一下这个方法

public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
AccessControlContext acc = null;
if(System.getSecurityManager() != null && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
acc = this.applicationContext.getBeanFactory().getAccessControlContext();
}

if(acc != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
ApplicationContextAwareProcessor.this.invokeAwareInterfaces(bean);
return null;
}
}, acc);
} else {
this.invokeAwareInterfaces(bean);
}

return bean;
}

进入这个invokeAwareInterfaces,我们之前说过,如果bean继承了Aware系列的类,会在调用init-method前调用前置方法,在这个前置方法中,再调用这个invokeAwareInterfaces,将资源进行注入。

private void invokeAwareInterfaces(Object bean) {
if(bean instanceof Aware) {
if(bean instanceof EnvironmentAware) {
((EnvironmentAware)bean).setEnvironment(this.applicationContext.getEnvironment());
}

if(bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware)bean).setEmbeddedValueResolver(new ApplicationContextAwareProcessor.EmbeddedValueResolver(this.applicationContext.getBeanFactory()));
}

if(bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware)bean).setResourceLoader(this.applicationContext);
}

if(bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware)bean).setApplicationEventPublisher(this.applicationContext);
}

if(bean instanceof MessageSourceAware) {
((MessageSourceAware)bean).setMessageSource(this.applicationContext);
}

if(bean instanceof ApplicationContextAware) {
((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);
}
}

}

继续回到之前的refresh方法

            try {
//这个方法可以有子类方法实现覆盖,然后对beanfactory进行处理,这个特性我们在spring中经常看见,使spring具有了可拓展性。
this.postProcessBeanFactory(beanFactory);
//激活各种BeanFactory处理器 this.invokeBeanFactoryPostProcessors(beanFactory);

来看一下这个方法,首先我们要知道BeanFactoryProcessor和BeanPostProcessor还是有区别的,BeanFactoryProcessor主要是在BeanFactory完成对bean的提取之后,对BeanFactory做的改动,主要还是BeanFactory中BeanDefinition的处理,而BeanPostProcessor在获取bean实例的过程中,可以对bean对象做相关改动。在这个方法中,有三个集合,分别是registryPostProcessors:来记录通过硬编码注册和配置文件注册的BeanDefinitionRegistryPostProcessor,regularPostProcessors来记录通过硬编码注册的BeanFactoryPostProcessor;BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子类,(spring4的这个方法和spring3可能不一样)

public static void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
Set<String> processedBeans = new HashSet();
int var9;
ArrayList priorityOrderedPostProcessors;
ArrayList orderedPostProcessors;
if(beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry)beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList();
List<BeanDefinitionRegistryPostProcessor> registryPostProcessors = new LinkedList();
//得到硬编码获得的后处理器集合的迭代器
Iterator var6 = beanFactoryPostProcessors.iterator();
//下面是对这写后处理器进行遍历
while(var6.hasNext()) {
BeanFactoryPostProcessor postProcessor = (BeanFactoryPostProcessor)var6.next();
//如果是BeanDefinitionRegistryPostProcessor
if(postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
//转换成BeanDefinitionRegistryPostProcessor类型
BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor)postProcessor;
//执行处理方法
registryPostProcessor.postProcessBeanDefinitionRegistry(registry);
//添加到registryPostProcessors中
registryPostProcessors.add(registryPostProcessor);
} else {
//添加到regularPostProcessors集合中
regularPostProcessors.add(postProcessor);
}
}
//获取容器里所有注册的BeanDefinitionRegistryPostProcessor的beanName
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
priorityOrderedPostProcessors = new ArrayList();
String[] var22 = postProcessorNames;
var9 = postProcessorNames.length;

int var10;
for(var10 = 0; var10 < var9; ++var10) {
String ppName = var22[var10];
//如果是PriorityOrdered类型,那么添加到priorityOrderedPostProcessors集合中
if(beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}

//按优先级排序 OrderComparator.sort(priorityOrderedPostProcessors);
//同时添加到registryPostProcessors集合,这时添加进去的是xml配置文件中获取的 registryPostProcessors.addAll(priorityOrderedPostProcessors);
//执行从xml配置中获取的处理器的postProcessBeanDefinitionRegistry方法
invokeBeanDefinitionRegistryPostProcessors(priorityOrderedPostProcessors, registry);
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
orderedPostProcessors = new ArrayList();
String[] var26 = postProcessorNames;
var10 = postProcessorNames.length;

int var30;
for(var30 = 0; var30 < var10; ++var30) {
String ppName = var26[var30];
//如果符合Ordered类型,就放到orderedPostProcessors集合中
if(!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
//按有优先级排序
OrderComparator.sort(orderedPostProcessors);
//添加到registryPostProcessors中 registryPostProcessors.addAll(orderedPostProcessors);
//执行集合的postProcessBeanDefinitionRegistry方法 invokeBeanDefinitionRegistryPostProcessors(orderedPostProcessors, registry);
boolean reiterate = true;
//接下来是把既没有实现PriorityOrdered也没有实现Ordered接口的BeanDefinitionRegistryPostProcessor执行那个处理方法,并添加在集合中,
while(reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
String[] var28 = postProcessorNames;
var30 = postProcessorNames.length;

for(int var33 = 0; var33 < var30; ++var33) {
String ppName = var28[var33];
if(!processedBeans.contains(ppName)) {
BeanDefinitionRegistryPostProcessor pp = (BeanDefinitionRegistryPostProcessor)beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class);
registryPostProcessors.add(pp);
processedBeans.add(ppName);
pp.postProcessBeanDefinitionRegistry(registry);
reiterate = true;
}
}
}

//执行registryPostProcessors集合中所有处理器的postProcessBeanFactory方法 invokeBeanFactoryPostProcessors((Collection)registryPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
//执行regularPostProcessors集合中所有处理器的postProcessBeanFactory方法 invokeBeanFactoryPostProcessors((Collection)regularPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
} else {
invokeBeanFactoryPostProcessors((Collection)beanFactoryPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
}
//下面的代码起作用是在beanFactory instanceof BeanDefinitionRegistry不成立的情况下,这样只是执行BeanFactoryPostProcessor下的方法,执行过程跟之前还是很相似的。
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList();
List<String> orderedPostProcessorNames = new ArrayList();
List<String> nonOrderedPostProcessorNames = new ArrayList();
String[] var21 = postProcessorNames;
int var24 = postProcessorNames.length;

String ppName;
for(var9 = 0; var9 < var24; ++var9) {
ppName = var21[var9];
if(!processedBeans.contains(ppName)) {
if(beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
} else if(beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
} else {
nonOrderedPostProcessorNames.add(ppName);
}
}
}

OrderComparator.sort(priorityOrderedPostProcessors);
invokeBeanFactoryPostProcessors((Collection)priorityOrderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
priorityOrderedPostProcessors = new ArrayList();
Iterator var25 = orderedPostProcessorNames.iterator();

while(var25.hasNext()) {
String postProcessorName = (String)var25.next();
priorityOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}

OrderComparator.sort(priorityOrderedPostProcessors);
invokeBeanFactoryPostProcessors((Collection)priorityOrderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
orderedPostProcessors = new ArrayList();
Iterator var32 = nonOrderedPostProcessorNames.iterator();

while(var32.hasNext()) {
ppName = (String)var32.next();
orderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}

invokeBeanFactoryPostProcessors((Collection)orderedPostProcessors, (ConfigurableListableBeanFactory)beanFactory);
}

激活完BeanFactory处理器后,接着回到之前的步骤

            //注册拦截bean创建的bean处理器,这里只是注册,整整调用是在bean创建的时候,也就是getBean,例如之前init方法前后的处理
this.registerBeanPostProcessors(beanFactory);

进入该方法,这也是ApplicationConetxt的一大亮点,beanfactory不会自动注册这些处理区,而ApplicationConetxt实现了自动注册。

public static void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
//获取所有实现BeanPostProcessor的beanName
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
beanFactory.addBeanPostProcessor(new PostProcessorRegistrationDelegate.BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList();
List<BeanPostProcessor> internalPostProcessors = new ArrayList();
List<String> orderedPostProcessorNames = new ArrayList();
List<String> nonOrderedPostProcessorNames = new ArrayList();
String[] var8 = postProcessorNames;
int var9 = postProcessorNames.length;
//将BeanPostProcessor添加到各自的集合中
String ppName;
BeanPostProcessor pp;
for(int var10 = 0; var10 < var9; ++var10) {
ppName = var8[var10];
if(beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
priorityOrderedPostProcessors.add(pp);
if(pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
} else if(beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
} else {
nonOrderedPostProcessorNames.add(ppName);
}
}

//注册所有实现了priorityOrdered的beanPostProcessors OrderComparator.sort(priorityOrderedPostProcessors);
registerBeanPostProcessors(beanFactory, (List)priorityOrderedPostProcessors);
//注册所有实现ordered的beanPostProcessors
List<BeanPostProcessor> orderedPostProcessors = new ArrayList();
Iterator var14 = orderedPostProcessorNames.iterator();

while(var14.hasNext()) {
String ppName = (String)var14.next();
BeanPostProcessor pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
orderedPostProcessors.add(pp);
if(pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}
//注册所有无序的beanPostProcessors
OrderComparator.sort(orderedPostProcessors);
registerBeanPostProcessors(beanFactory, (List)orderedPostProcessors);
List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList();
Iterator var17 = nonOrderedPostProcessorNames.iterator();

while(var17.hasNext()) {
ppName = (String)var17.next();
pp = (BeanPostProcessor)beanFactory.getBean(ppName, BeanPostProcessor.class);
nonOrderedPostProcessors.add(pp);
if(pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
}

registerBeanPostProcessors(beanFactory, (List)nonOrderedPostProcessors);
//注册所有MergedBeanDefinitionPostProcessor类型的BeanPostProcessor
OrderComparator.sort(internalPostProcessors);
registerBeanPostProcessors(beanFactory, (List)internalPostProcessors);
//添加ApplicationListener探测器
beanFactory.addBeanPostProcessor(new PostProcessorRegistrationDelegate.ApplicationListenerDetector(applicationContext));
}

回到之前代码

                //为上下文初始化message源,就是国际化处理
this.initMessageSource();

进入方法

protected void initMessageSource() {
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
/*国际化需要这么一个bean,从下面这段编码看出,这个bean的id必须为messageSource,否则就会有问题
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
*/

if(beanFactory.containsLocalBean("messageSource")) {
//将bean重提取中这个信息资源实例,并设置资源
this.messageSource = (MessageSource)beanFactory.getBean("messageSource", MessageSource.class);
if(this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
HierarchicalMessageSource hms = (HierarchicalMessageSource)this.messageSource;
if(hms.getParentMessageSource() == null) {
hms.setParentMessageSource(this.getInternalParentMessageSource());
}
}

if(this.logger.isDebugEnabled()) {
this.logger.debug("Using MessageSource [" + this.messageSource + "]");
}
} else {
//如果用户并没有定义配置文件键,那么用临时的DelegatingMessageSource作为资源返回
DelegatingMessageSource dms = new DelegatingMessageSource();
dms.setParentMessageSource(this.getInternalParentMessageSource());
this.messageSource = dms;
beanFactory.registerSingleton("messageSource", this.messageSource);
if(this.logger.isDebugEnabled()) {
this.logger.debug("Unable to locate MessageSource with name 'messageSource': using default [" + this.messageSource + "]");
}
}

}

context.getMessage(“test”,params, Locale.CHINA)这是获取了这个设置进去的资源实例,然后从对应的资源中提取属性,并格式化
接着看下面的过程

                //初始化应用消息广播器
this.initApplicationEventMulticaster();

进入该方法,如果用户自定义了时间广播器,那么就用用户自定义的,如果没有就模默认的SimpleApplicationEventMulticaster,那么是如何进行广播的呢,了解过设计模式的会想到观察者模式,当发生事件时,来遍历所有的监听器,将事件传入,调用监听器的方法,这个这个监听器的方法是onApplicationEvent

protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
if(beanFactory.containsLocalBean("applicationEventMulticaster")) {
this.applicationEventMulticaster = (ApplicationEventMulticaster)beanFactory.getBean("applicationEventMulticaster", ApplicationEventMulticaster.class);
if(this.logger.isDebugEnabled()) {
this.logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
} else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton("applicationEventMulticaster", this.applicationEventMulticaster);
if(this.logger.isDebugEnabled()) {
this.logger.debug("Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [" + this.applicationEventMulticaster + "]");
}
}

}

回到refresh

                //留给子类实现,来初始化其他的bean
this.onRefresh();
//注册监听器
this.registerListeners();

进入该方法

protected void registerListeners() {
//处理硬编码方式的监听器
Iterator var1 = this.getApplicationListeners().iterator();

while(var1.hasNext()) {
ApplicationListener<?> listener = (ApplicationListener)var1.next();
//添加到广播器的事件监听器容器中 this.getApplicationEventMulticaster().addApplicationListener(listener);
}
//获取配置文件中的监听器
String[] listenerBeanNames = this.getBeanNamesForType(ApplicationListener.class, true, false);
String[] var7 = listenerBeanNames;
int var3 = listenerBeanNames.length;

for(int var4 = 0; var4 < var3; ++var4) {
String lisName = var7[var4];
this.getApplicationEventMulticaster().addApplicationListenerBean(lisName);
}

}

回到refresh

        //首先会初始化属性转换器Converter,然后冻结bean的定义,使bean的定义无法修改,然后加载所有非延迟的单例
this.finishBeanFactoryInitialization(beanFactory);
//初始化生命周期类,发布事件
this.finishRefresh();
} catch (BeansException var5) {
this.destroyBeans();
this.cancelRefresh(var5);
throw var5;
}

}
}