Spring事件機制源碼的案例分析?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純热荩屛覀円黄饋砜纯窗桑?/p>

PS:Spring版本為5.1.5.RELEASE
源碼分析
初始化
初始化這塊關鍵是核心組件的注冊
1、ApplicationEventPublisher的初始化與注冊,關鍵方法為AbstractApplicationContext的方法prepareBeanFactory()
2、ApplicationEventMulticaster的初始化與注冊,關鍵方法為AbstractApplicationContext的initApplicationEventMulticaster()方法
3、ApplicationListener的初始化與注冊,關鍵方法為AbstractApplicationContext的registerListeners()方法
這塊不細說,感興趣的可以自行跟蹤關鍵方法
事件發(fā)布/訂閱
事件發(fā)布/訂閱的關鍵方法為AbstractApplicationContext的publishEvent,源碼如下:
protected void publishEvent(Object event, ResolvableType eventType) {
// 避免空指針
Assert.notNull(event, "Event must not be null");
if (logger.isTraceEnabled()) {
logger.trace("Publishing event in " + getDisplayName() + ": " + event);
}
// 處理event對象,將其轉換為ApplicationEvent
ApplicationEvent applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
else {
applicationEvent = new PayloadApplicationEvent<Object>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
}
}
// 是否延遲多播,即將事件發(fā)布到所有監(jiān)聽器中
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
else {
//此處為事件監(jiān)聽處理器的調用關鍵
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
}
// 是否將事件發(fā)布到父容器中
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
}
else {
this.parent.publishEvent(event);
}
}
}通過代碼跟蹤,發(fā)現Spring中使用ApplicationEventMulticaster的默認實現SimpleApplicationEventMulticaster來觸發(fā)事件的監(jiān)聽,關鍵方法為multicastEvent()方法,源碼如下:
@Override
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
// 獲取事件類型
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {//依次遍歷事件監(jiān)聽器
// 獲取線程池
Executor executor = getTaskExecutor();
if (executor != null) {//線程池不為null,則異步調用監(jiān)聽器
executor.execute(new Runnable() {
@Override
public void run() {
invokeListener(listener, event);
}
});
}
else {// 同步調用監(jiān)聽器
invokeListener(listener, event);
}
}
}感謝各位的閱讀!看完上述內容,你們對Spring事件機制源碼的案例分析大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注創(chuàng)新互聯行業(yè)資訊頻道。
本文題目:Spring事件機制源碼的案例分析-創(chuàng)新互聯
文章地址:http://chinadenli.net/article42/pssec.html
成都網站建設公司_創(chuàng)新互聯,為您提供ChatGPT、品牌網站設計、做網站、品牌網站制作、動態(tài)網站、Google
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯