SpringBoot深入分析講解監(jiān)聽器模式上
注:圖片來源于網(wǎng)絡(luò)
SpringBoot作為業(yè)內(nèi)公認(rèn)的優(yōu)秀開源框架,它的監(jiān)聽器是如何實(shí)現(xiàn)呢?在這里首先對(duì)一些基礎(chǔ)組件進(jìn)行分析;
1、事件ApplicationEvent
ApplicationEvent是一個(gè)抽象類,idea上展開其繼承關(guān)系如圖:
可見SpringBoot所定義的事件類型是極為豐富的。
2、監(jiān)聽器ApplicationListener
ApplicationListener是一個(gè)接口,我們也可以通過實(shí)現(xiàn)這個(gè)接口來定義自己的監(jiān)聽器,可以通過與事件初始化器方式相似的方式進(jìn)行加載。
@FunctionalInterface public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { /** * Handle an application event. * @param event the event to respond to */ void onApplicationEvent(E event); }
我們可以看到代碼中它接受一個(gè)上文中提到的事件泛型,這代表了此監(jiān)聽器關(guān)注的事件;
還有一種實(shí)現(xiàn)監(jiān)聽器的方式,即實(shí)現(xiàn)SmartApplicationListener接口,SmartApplicationListener繼承了ApplicationListener接口,通過這種方式實(shí)現(xiàn)監(jiān)聽器,可以同時(shí)注冊(cè)多個(gè)感興趣的事件,只需實(shí)現(xiàn)接口的supportsEventType方法即可;
public interface SmartApplicationListener extends ApplicationListener<ApplicationEvent>, Ordered { /** * Determine whether this listener actually supports the given event type. * @param eventType the event type (never {@code null}) */ boolean supportsEventType(Class<? extends ApplicationEvent> eventType); /** * Determine whether this listener actually supports the given source type. * <p>The default implementation always returns {@code true}. * @param sourceType the source type, or {@code null} if no source */ default boolean supportsSourceType(@Nullable Class<?> sourceType) { return true; } /** * Determine this listener's order in a set of listeners for the same event. * <p>The default implementation returns {@link #LOWEST_PRECEDENCE}. */ @Override default int getOrder() { return LOWEST_PRECEDENCE; } }
3、事件廣播器ApplicationEventMulticaster
ApplicationEventMulticaster是一個(gè)接口,定義了添加監(jiān)聽器、刪除監(jiān)聽器、傳播事件等方法;
SpringBoot為我們實(shí)現(xiàn)了SimpleApplicationEventMulticaster這一事件廣播器,繼承關(guān)系如圖所示:
SpringBoot如何傳播事件,有時(shí)間在下一篇博文進(jìn)行整理,本文有哪些不對(duì)之處,也感謝大家的指正。
到此這篇關(guān)于SpringBoot深入分析講解監(jiān)聽器模式上的文章就介紹到這了,更多相關(guān)SpringBoot監(jiān)聽器模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?數(shù)據(jù)交換?Json?和?異步請(qǐng)求?Ajax詳解
Json(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,采用鍵值對(duì)的形式來表示數(shù)據(jù),它廣泛應(yīng)用于Web開發(fā)中,特別適合于前后端數(shù)據(jù)傳輸和存儲(chǔ),這篇文章主要介紹了Java數(shù)據(jù)交換Json和異步請(qǐng)求Ajax,需要的朋友可以參考下2023-09-09SpringBoot JPA出現(xiàn)錯(cuò)誤:No identifier specified&nb
這篇文章主要介紹了SpringBoot JPA出現(xiàn)錯(cuò)誤:No identifier specified for en解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java中ShardingSphere 數(shù)據(jù)分片的實(shí)現(xiàn)
其實(shí)很多人對(duì)分庫(kù)分表多少都有點(diǎn)恐懼,我們今天用ShardingSphere 給大家演示數(shù)據(jù)分片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09海量數(shù)據(jù)去重排序bitmap(位圖法)在java中實(shí)現(xiàn)的兩種方法
今天小編就為大家分享一篇關(guān)于海量數(shù)據(jù)去重排序bitmap(位圖法)在java中實(shí)現(xiàn)的兩種方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02