spring
前沿:感觉所有重点都在图里面了
需要懂的技术论点
- springbootstrap方法执行
- 类加载过程
- proxy的代理模式
- relection框架的过程
- spring的模块包干的事情
listeners.starting()干的事情
这一块 自己都没特别看懂🤦♂️,还是以类图的层次来主要分析吧
概念总结:
SpringApplicationRunListener:通过调用SimpleApplicationEventMulticaster类型的启动
SimpleApplicationEventMulticaster:一个简单的事件启动类,拥有简单的启动功能,和容器的存取功能
EventPublishingRunListener:
上述类的关系有阐述清楚,SpringApplicationRunListeners中有关于SpringApplicationRunListener容器,EventPublishingRunListener 是 上面runListener的具体实现类,它拥有SimpleApplicationEventMulticaster并且通过他去启动。
SpringApplicationRunListener
主要就是
SpringApplicationRunListener listener = (SpringApplicationRunListener)var1.next();
listener.starting()
并且在SpringApplicationRunListener里面有一段
public void starting() {
this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
}
这个就是核心。
继续进入里面分析
public void multicastEvent(ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
Iterator var4 = this.getApplicationListeners(event, type).iterator();
while(var4.hasNext()) {
ApplicationListener<?> listener = (ApplicationListener)var4.next();
Executor executor = this.getTaskExecutor();
if (executor != null) {
executor.execute(() -> {
this.invokeListener(listener, event);
});
} else {
this.invokeListener(listener, event);
}
}
}
这段代码里面 有两个核心,里面的值从哪里来,方法真正的执行是什么。
- 第一个值从哪里来
this.getApplicationListeners(event, type).iterator();
protected Collection<ApplicationListener<?>> getApplicationListeners(ApplicationEvent event, ResolvableType eventType) {
Object source = event.getSource();
Class<?> sourceType = source != null ? source.getClass() : null;
AbstractApplicationEventMulticaster.ListenerCacheKey cacheKey = new AbstractApplicationEventMulticaster.ListenerCacheKey(eventType, sourceType);
AbstractApplicationEventMulticaster.ListenerRetriever retriever = (AbstractApplicationEventMulticaster.ListenerRetriever)this.retrieverCache.get(cacheKey);
if (retriever != null) {
return retriever.getApplicationListeners();
} else if (this.beanClassLoader == null || ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) && (sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader))) {
synchronized(this.retrievalMutex) {
retriever = (AbstractApplicationEventMulticaster.ListenerRetriever)this.retrieverCache.get(cacheKey);
if (retriever != null) {
return retriever.getApplicationListeners();
} else {
retriever = new AbstractApplicationEventMulticaster.ListenerRetriever(true);
Collection<ApplicationListener<?>> listeners = this.retrieveApplicationListeners(eventType, sourceType, retriever);
this.retrieverCache.put(cacheKey, retriever);
return listeners;
}
}
} else {
return this.retrieveApplicationListeners(eventType, sourceType, (AbstractApplicationEventMulticaster.ListenerRetriever)null);
}
}
AbstractApplicationEventMulticaster专门为,事件定义个一个私有类,在里面进行存取的操作。里面的核心就是
ListenerRetriever.applicationListeners和ListenerRetriever.applicationListeners.applicationListenerBeans这两个可爱的小东东哈
2. 方法真正的执行是什么
this.invokeListener(listener, event)
protected void invokeListener(ApplicationListener<?> listener, ApplicationEvent event) {
ErrorHandler errorHandler = this.getErrorHandler();
if (errorHandler != null) {
try {
this.doInvokeListener(listener, event);
} catch (Throwable var5) {
errorHandler.handleError(var5);
}
} else {
this.doInvokeListener(listener, event);
}
}
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
listener.onApplicationEvent(event);
} catch (ClassCastException var6) {
String msg = var6.getMessage();
if (msg != null && !this.matchesClassCastMessage(msg, event.getClass())) {
throw var6;
}
Log logger = LogFactory.getLog(this.getClass());
if (logger.isTraceEnabled()) {
logger.trace("Non-matching event type for listener: " + listener, var6);
}
}
}
这两个方法,最后listener.onApplicationEvent(event)怎么去分析,那就太多了,因为不同的实现,有不同的具体逻辑就不继续看了