Springboot四種事件監(jiān)聽(tīng)的實(shí)現(xiàn)方式詳解
前言
講到事件監(jiān)聽(tīng),這里我們說(shuō)下自定義事件和自定義監(jiān)聽(tīng)器類(lèi)的實(shí)現(xiàn)方式:
自定義事件:繼承自ApplicationEvent抽象類(lèi),然后定義自己的構(gòu)造器
自定義監(jiān)聽(tīng):實(shí)現(xiàn)ApplicationListener<T>接口,然后實(shí)現(xiàn)onApplicationEvent方法
下面講下4種事件監(jiān)聽(tīng)的具體實(shí)現(xiàn)
手工向ApplicationContext中添加監(jiān)聽(tīng)器
首先創(chuàng)建MyListener1類(lèi)
public class MyListener1 implements ApplicationListener<MyEvent>{
Logger logger = Logger.getLogger(MyListener1.class);
public void onApplicationEvent(MyEvent event){
logger.info(String.format("%s監(jiān)聽(tīng)到事件源:%s.", MyListener1.class.getName(), event.getSource()));
}
}然后在springboot應(yīng)用啟動(dòng)類(lèi)中獲取ConfigurableApplicationContext上下文,裝載監(jiān)聽(tīng)
@SpringBootApplication
public class LisenterApplication{
public static void main(String[] args){
ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args);
//裝載監(jiān)聽(tīng)
context.addApplicationListener(new MyListener1());
}
}將監(jiān)聽(tīng)器裝載入spring容器
創(chuàng)建MyListener2類(lèi),并使用@Component注解將該類(lèi)裝載入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)聽(tīng)到事件源:%s.", MyListener2.class.getName(), event.getSource()));
}
}在application.properties中配置監(jiān)聽(tīng)器
首先創(chuàng)建MyListener3類(lèi)
public class MyListener3 implements ApplicationListener<MyEvent>{
Logger logger = Logger.getLogger(MyListener3.class);
public void onApplicationEvent(MyEvent event){
logger.info(String.format("%s監(jiān)聽(tīng)到事件源:%s.", MyListener3.class.getName(), event.getSource()));
}
}然后在application.properties中配置監(jiān)聽(tīng)
context.listener.classes=com.listener.MyListener3
通過(guò)@EventListener注解實(shí)現(xiàn)事件監(jiān)聽(tīng)
創(chuàng)建MyListener4類(lèi),該類(lèi)無(wú)需實(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)聽(tīng)到事件源:%s.", MyListener4.class.getName(), event.getSource()));
}
}自定義事件代碼如下:
public class MyEvent extends ApplicationEvent{
public MyEvent(Object source)
{
super(source);
}
}進(jìn)行測(cè)試(在啟動(dòng)類(lèi)中加入發(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("測(cè)試事件."));
//發(fā)布事件方式2
applicationEventPublisher.publishEvent(new MyEvent("測(cè)試事件."));
//發(fā)布事件方式3
applicationContext.publishEvent(new MyEvent("測(cè)試事件."));
}
}啟動(dòng)后,日志打印如下:
2018-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener3 : com.listener.MyListener3監(jiān)聽(tīng)到事件源:測(cè)試事件..
2018-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener4 : com.listener.MyListener4監(jiān)聽(tīng)到事件源:測(cè)試事件..
2018-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener2 : com.listener.MyListener2監(jiān)聽(tīng)到事件源:測(cè)試事件..
2018-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener1 : com.listener.MyListener1監(jiān)聽(tīng)到事件源:測(cè)試事件..
由日志打印可以看出,SpringBoot四種事件的實(shí)現(xiàn)方式監(jiān)聽(tīng)是有序的
到此這篇關(guān)于Springboot四種事件監(jiān)聽(tīng)的實(shí)現(xiàn)方式詳解的文章就介紹到這了,更多相關(guān)Springboot事件監(jiān)聽(tīng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java面向?qū)ο箢?lèi)和對(duì)象實(shí)例詳解
面向?qū)ο竽耸荍ava語(yǔ)言的核心,是程序設(shè)計(jì)的思想,這篇文章主要介紹了Java面向?qū)ο箢?lèi)和對(duì)象的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
HttpServletRequestWrapper干預(yù)Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請(qǐng)求的流程中干預(yù)?Request對(duì)象,?通過(guò)基于HttpServletRequestWrapper和?Filter組合進(jìn)行干預(yù),有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09
java獲取redis日志信息與動(dòng)態(tài)監(jiān)控信息的方法
這篇文章主要給大家介紹了關(guān)于java如何獲取redis日志信息與動(dòng)態(tài)監(jiān)控信息的方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04
Java調(diào)用微信客服消息實(shí)現(xiàn)發(fā)貨通知的方法詳解
這篇文章主要介紹了Java調(diào)用微信客服消息實(shí)現(xiàn)發(fā)貨通知的方法,結(jié)合實(shí)例形式詳細(xì)分析了java針對(duì)微信接口調(diào)用的原理、調(diào)用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
通過(guò)反射注解批量插入數(shù)據(jù)到DB的實(shí)現(xiàn)方法
今天小編就為大家分享一篇關(guān)于通過(guò)反射注解批量插入數(shù)據(jù)到DB的實(shí)現(xiàn)方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
使用自定義注解+springAop實(shí)現(xiàn)參數(shù)非空校驗(yàn)方式
這篇文章主要介紹了使用自定義注解+springAop實(shí)現(xiàn)參數(shù)非空校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

