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)用啟動類中獲取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)行測試(在啟動類中加入發(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("測試事件."));
}
}啟動后,日志打印如下:
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
HttpServletRequestWrapper干預(yù)Request處理流程解析
這篇文章主要分析在?Tomcat的處理?http?請求的流程中干預(yù)?Request對象,?通過基于HttpServletRequestWrapper和?Filter組合進(jìn)行干預(yù),有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09
java獲取redis日志信息與動態(tài)監(jiān)控信息的方法
這篇文章主要給大家介紹了關(guān)于java如何獲取redis日志信息與動態(tài)監(jiān)控信息的方法,文中介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04
Java調(diào)用微信客服消息實(shí)現(xiàn)發(fā)貨通知的方法詳解
這篇文章主要介紹了Java調(diào)用微信客服消息實(shí)現(xiàn)發(fā)貨通知的方法,結(jié)合實(shí)例形式詳細(xì)分析了java針對微信接口調(diào)用的原理、調(diào)用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
通過反射注解批量插入數(shù)據(jù)到DB的實(shí)現(xiàn)方法
今天小編就為大家分享一篇關(guān)于通過反射注解批量插入數(shù)據(jù)到DB的實(shí)現(xiàn)方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
使用自定義注解+springAop實(shí)現(xiàn)參數(shù)非空校驗(yàn)方式
這篇文章主要介紹了使用自定義注解+springAop實(shí)現(xiàn)參數(shù)非空校驗(yàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

