Spring事件機(jī)制源碼的案例分析?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過(guò)這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!
PS:Spring版本為5.1.5.RELEASE
源碼分析
初始化
初始化這塊關(guān)鍵是核心組件的注冊(cè)
1、ApplicationEventPublisher的初始化與注冊(cè),關(guān)鍵方法為AbstractApplicationContext的方法prepareBeanFactory()
2、ApplicationEventMulticaster的初始化與注冊(cè),關(guān)鍵方法為AbstractApplicationContext的initApplicationEventMulticaster()方法
3、ApplicationListener的初始化與注冊(cè),關(guān)鍵方法為AbstractApplicationContext的registerListeners()方法
這塊不細(xì)說(shuō),感興趣的可以自行跟蹤關(guān)鍵方法
事件發(fā)布/訂閱
事件發(fā)布/訂閱的關(guān)鍵方法為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對(duì)象,將其轉(zhuǎn)換為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)聽處理器的調(diào)用關(guā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); } } }
通過(guò)代碼跟蹤,發(fā)現(xiàn)Spring中使用ApplicationEventMulticaster的默認(rèn)實(shí)現(xiàn)SimpleApplicationEventMulticaster來(lái)觸發(fā)事件的監(jiān)聽,關(guā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,則異步調(diào)用監(jiān)聽器 executor.execute(new Runnable() { @Override public void run() { invokeListener(listener, event); } }); } else {// 同步調(diào)用監(jiān)聽器 invokeListener(listener, event); } } }
感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)Spring事件機(jī)制源碼的案例分析大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:Spring事件機(jī)制源碼的案例分析-創(chuàng)新互聯(lián)
文章地址:http://jinyejixie.com/article42/pssec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、品牌網(wǎng)站設(shè)計(jì)、做網(wǎng)站、品牌網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容