You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
protectedvoidregisterListeners() {
// Register statically specified listeners first.// 读取 ApplicationListenerfor (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans// uninitialized to let post-processors apply to them!/** * 寻找类型为{@link ApplicationListener} 的beanName,目标文件为用户配置文件 */String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (StringlistenerBeanName : listenerBeanNames) {
/** * 1. 获取 {@link applicationEventMulticaster} * 2. 添加监听器名称 */getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEventearlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
finishRefresh 发布
protectedvoidfinishRefresh() {
// Clear context-level resource caches (such as ASM metadata from scanning).clearResourceCaches();
// Initialize lifecycle processor for this context.initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.getLifecycleProcessor().onRefresh();
// Publish the final event.// 发布事件做处理publishEvent(newContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.LiveBeansView.registerApplicationContext(this);
}
@SuppressWarnings({"rawtypes", "unchecked"})
privatevoiddoInvokeListener(ApplicationListenerlistener, ApplicationEventevent) {
try {
// 最后调用方法listener.onApplicationEvent(event);
}
catch (ClassCastExceptionex) {
Stringmsg = ex.getMessage();
if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type for// -> let's suppress the exception and just log a debug message.Loglogger = LogFactory.getLog(getClass());
if (logger.isTraceEnabled()) {
logger.trace("Non-matching event type for listener: " + listener, ex);
}
}
else {
throwex;
}
}
}