SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解
ApplicationListener
ApplicationListener是應(yīng)用程序的事件監(jiān)聽器,繼承自java.util.EventListener標(biāo)準(zhǔn)接口,采用觀察者設(shè)計(jì)模式。
從spring3.0開始,ApplicationListener可以指定要監(jiān)聽的事件類型,當(dāng)spring的上下文環(huán)境ApplicationContext時(shí),當(dāng)指定事件發(fā)布的時(shí)候,對(duì)應(yīng)事件的事件監(jiān)聽器將被調(diào)用。
使用ApplicationListener需要三元素,事件,監(jiān)聽,事件發(fā)布。
ApplicationListener應(yīng)用
1.新建事件
繼承自ApplicationEvent .
public class TestApplicationEvent extends ApplicationEvent { /** * Create a new ApplicationEvent. * * @param test the object on which the event initially occurred (never {@code null}) */ public TestApplicationEvent(TestApplication test) { super(test); } }
2.新建監(jiān)聽器
實(shí)現(xiàn)ApplicationListener指定要監(jiān)聽的事件類型,監(jiān)聽器必須注入容器當(dāng)中可以使用注解方式(例如:@Component),或者在資源路徑下新建META-INF/spring.factories文件導(dǎo)入
@Component public class TestApplicationListener implements ApplicationListener<TestApplicationEvent> { @Override public void onApplicationEvent(TestApplicationEvent event) { System.out.println(event.getSource()); } }
3.事件發(fā)布
當(dāng)spring應(yīng)用上下文環(huán)境初始化完畢,進(jìn)行事件的發(fā)布
@SpringBootApplication public class CommonsTestApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication .run(CommonsTestApplication.class, args); TestApplication testApplication = new TestApplication(); testApplication.setName("zhangsan"); testApplication.setAge(23); TestApplicationEvent testApplicationEvent = new TestApplicationEvent(testApplication); applicationContext.publishEvent(testApplicationEvent);//事件發(fā)布時(shí),指定的監(jiān)聽器就進(jìn)行接收 } }
4.啟動(dòng)程序
當(dāng)容器初始化完畢后,發(fā)布事件,事件監(jiān)聽器進(jìn)行接收。
到此這篇關(guān)于SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解的文章就介紹到這了,更多相關(guān)ApplicationListener事件監(jiān)聽器使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot監(jiān)聽?wèi)?yīng)用程序啟動(dòng)的生命周期事件的四種方法
- SpringBoot使用Redis單機(jī)版過期鍵監(jiān)聽事件的實(shí)現(xiàn)示例
- springboot創(chuàng)建監(jiān)聽和處理事件的操作方法
- Java?Springboot異步執(zhí)行事件監(jiān)聽和處理實(shí)例
- SpringBoot如何監(jiān)聽redis?Key變化事件案例詳解
- 詳解SpringBoot實(shí)現(xiàn)事件同步與異步監(jiān)聽
- SpringBoot?對(duì)接飛書多維表格事件回調(diào)監(jiān)聽流程分析
相關(guān)文章
java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10Tomcat 服務(wù)器 在45秒內(nèi)未啟動(dòng)成功的解決方法
下面小編就為大家?guī)硪黄猅omcat 服務(wù)器 在45秒內(nèi)未啟動(dòng)成功的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Mybatis_plus基礎(chǔ)教程(總結(jié)篇)
這篇文章主要介紹了Mybatis_plus基礎(chǔ)教程(總結(jié)篇),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Java任務(wù)定時(shí)執(zhí)行器案例的實(shí)現(xiàn)
定時(shí)器會(huì)執(zhí)行指定的任務(wù),本文主要介紹了Java任務(wù)定時(shí)執(zhí)行器案例的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Java 互相關(guān)聯(lián)的實(shí)體無限遞歸問題的解決
這篇文章主要介紹了Java 互相關(guān)聯(lián)的實(shí)體無限遞歸問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java基于面向?qū)ο髮?shí)現(xiàn)一個(gè)戰(zhàn)士小游戲
這篇文章主要為大家詳細(xì)介紹了Java如何基于面向?qū)ο髮?shí)現(xiàn)一個(gè)戰(zhàn)士小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-07-07OpenFeign設(shè)置header的三種方式總結(jié)
在微服務(wù)間使用Feign進(jìn)行遠(yuǎn)程調(diào)用時(shí)需要在header中添加信息,下面這篇文章主要給大家介紹了關(guān)于OpenFeign設(shè)置header的三種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04