Springboot四種事件監(jiān)聽的實(shí)現(xiàn)方式詳解
前言
講到事件監(jiān)聽,這里我們說下自定義事件和自定義監(jiān)聽器類的實(shí)現(xiàn)方式:
自定義事件:繼承自ApplicationEvent抽象類,然后定義自己的構(gòu)造器
自定義監(jiān)聽:實(shí)現(xiàn)ApplicationListener<T>接口,然后實(shí)現(xiàn)onApplicationEvent方法
下面講下4種事件監(jiān)聽的具體實(shí)現(xiàn)
手工向ApplicationContext中添加監(jiān)聽器
首先創(chuàng)建MyListener1類
public class MyListener1 implements ApplicationListener<MyEvent>{ Logger logger = Logger.getLogger(MyListener1.class); public void onApplicationEvent(MyEvent event){ logger.info(String.format("%s監(jiān)聽到事件源:%s.", MyListener1.class.getName(), event.getSource())); } }
然后在springboot應(yīng)用啟動(dòng)類中獲取ConfigurableApplicationContext上下文,裝載監(jiān)聽
@SpringBootApplication public class LisenterApplication{ public static void main(String[] args){ ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args); //裝載監(jiān)聽 context.addApplicationListener(new MyListener1()); } }
將監(jiān)聽器裝載入spring容器
創(chuàng)建MyListener2類,并使用@Component注解將該類裝載入spring容器中
@Component public class MyListener2 implements ApplicationListener<MyEvent>{ Logger logger = Logger.getLogger(MyListener2.class); public void onApplicationEvent(MyEvent event) { logger.info(String.format("%s監(jiān)聽到事件源:%s.", MyListener2.class.getName(), event.getSource())); } }
在application.properties中配置監(jiān)聽器
首先創(chuàng)建MyListener3類
public class MyListener3 implements ApplicationListener<MyEvent>{ Logger logger = Logger.getLogger(MyListener3.class); public void onApplicationEvent(MyEvent event){ logger.info(String.format("%s監(jiān)聽到事件源:%s.", MyListener3.class.getName(), event.getSource())); } }
然后在application.properties中配置監(jiān)聽
context.listener.classes=com.listener.MyListener3
通過@EventListener注解實(shí)現(xiàn)事件監(jiān)聽
創(chuàng)建MyListener4類,該類無需實(shí)現(xiàn)ApplicationListener接口,使用@EventListener裝飾具體方法
@Component public class MyListener4{ Logger logger = Logger.getLogger(MyListener4.class); @EventListener public void listener(MyEvent event){ logger.info(String.format("%s監(jiān)聽到事件源:%s.", MyListener4.class.getName(), event.getSource())); } }
自定義事件代碼如下:
public class MyEvent extends ApplicationEvent{ public MyEvent(Object source) { super(source); } }
進(jìn)行測試(在啟動(dòng)類中加入發(fā)布事件的邏輯):
@SpringBootApplication public class LisenterApplication{ @Autowired private ApplicationEventPublisher applicationEventPublisher; @Autowired private ApplicationContext applicationContext; public static void main(String[] args){ ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args); //裝載事件 context.addApplicationListener(new MyListener1()); //發(fā)布事件方式1 context.publishEvent(new MyEvent("測試事件.")); //發(fā)布事件方式2 applicationEventPublisher.publishEvent(new MyEvent("測試事件.")); //發(fā)布事件方式3 applicationContext.publishEvent(new MyEvent("測試事件.")); } }
啟動(dòng)后,日志打印如下:
2018-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener3 : com.listener.MyListener3監(jiān)聽到事件源:測試事件..
2018-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener4 : com.listener.MyListener4監(jiān)聽到事件源:測試事件..
2018-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener2 : com.listener.MyListener2監(jiān)聽到事件源:測試事件..
2018-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener1 : com.listener.MyListener1監(jiān)聽到事件源:測試事件..
由日志打印可以看出,SpringBoot四種事件的實(shí)現(xiàn)方式監(jiān)聽是有序的
到此這篇關(guān)于Springboot四種事件監(jiān)聽的實(shí)現(xiàn)方式詳解的文章就介紹到這了,更多相關(guān)Springboot事件監(jiān)聽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea 實(shí)現(xiàn)搜索jdk中的類和包操作
這篇文章主要介紹了idea 實(shí)現(xiàn)搜索jdk中的類和包操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java找不到或無法加載主類及編碼錯(cuò)誤問題的解決方案
今天小編就為大家分享一篇關(guān)于Java找不到或無法加載主類及編碼錯(cuò)誤問題的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02SpringBoot如何使用ApplicationContext獲取bean對(duì)象
這篇文章主要介紹了SpringBoot 如何使用ApplicationContext獲取bean對(duì)象,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11解決 java.lang.NoSuchMethodError的錯(cuò)誤
這篇文章主要介紹了解決 java.lang.NoSuchMethodError的錯(cuò)誤的相關(guān)資料,需要的朋友可以參考下2017-06-06Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(57)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08詳解SpringBoot時(shí)間參數(shù)處理完整解決方案
這篇文章主要介紹了詳解SpringBoot時(shí)間參數(shù)處理完整解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12