通过上篇文章ApplicationListener与ApplicationEvent 使用及源码解析(上)可知,监听器列表是从defaultRetriever中的applicationListeners中获取的,那么applicationListeners是什么时候被添加进来的呢
本文就回答下该问题:实际就是从创建AnnotationConfigApplicationContext的时候被添加进来的
1.new AnnotationConfigApplicationContext()相关操作
public AnnotationConfigApplicationContext(Class... annotatedClasses) {
this();
register(annotatedClasses);
refresh();
}
2.跟踪到refresh(),具体实现如下:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
...
// 具体实现在这个方法
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
...
}
}
3.跟踪registerListeners()方法,具体如下:
protected void registerListeners() {
// Register statically specified listeners first.
for (ApplicationListener listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// 获取所有ApplicationListener相关的类名称列表
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
// 添加到defaultRetriever的applicationListenerBeans中
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
Set earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
4.跟踪getBeanNamesForType()方法,具体如下:
实现类为DefaultListableBeanFactory
@Override
public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) {
if (!isConfigurationFrozen() || type == null || !allowEagerInit) {
// 具体获取的方法
return doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, allowEagerInit);
}
...
return resolvedBeanNames;
}
private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
List result = new ArrayList();
// beanDefinitionNames 是所有被注册的bean
for (String beanName : this.beanDefinitionNames) {
// Only consider bean as eligible if the bean name
// is not defined as alias for some other bean.
if (!isAlias(beanName)) {
try {
...
if (!matchFound && isFactoryBean) {
// In case of FactoryBean, try to match FactoryBean instance itself next.
beanName = FACTORY_BEAN_PREFIX + beanName;
// 判断类型是否匹配
matchFound = (includeNonSingletons || mbd.isSingleton()) && isTypeMatch(beanName, type);
}
// 如果匹配,则添加到result中
if (matchFound) {
result.add(beanName);
}
}
}
...
}
}
// Check manually registered singletons too.
...
return StringUtils.toStringArray(result);
}
通过以上分析可知:在创建AnnotationConfigApplicationContext的时候,就把所有的监听器添加到defaultRetriever中了