springboot 事件監(jiān)聽的實(shí)現(xiàn)方法
定義事件
@Getter public class TestEvent extends ApplicationEvent { private String msg; public TestEvent(Object source, String msg) { super(source); this.msg = msg; } }
定義事件監(jiān)聽(注解方式)
@Component public class TestListen { @EventListener public void testListen(TestEvent event) { System.out.println(event.getMsg()); } }
注意:@Component 注解
發(fā)布事件
@Autowired private ApplicationContext publiser; @GetMapping("test-listen") public void testListen() { for (int i = 0; i < 10; i++) { System.out.println("i = " + i); } publiser.publishEvent(new TestEvent(this, "測試事件監(jiān)聽")); for (int j = 0; j < 10; j++) { System.out.println("j = " + j); } }
測試時(shí)執(zhí)行順序:
- i循環(huán)
- 打印"event = [測試事件監(jiān)聽]"
- j循環(huán)
異步監(jiān)聽
監(jiān)聽加上@Async注解
@Component public class TestListen { @EventListener @Async public void testListen(TestEvent event) { for (int i = 0; i < 10; i++) { System.out.println("event = [" + event.getMsg() + "]"); } } }
測試時(shí)執(zhí)行順序:
- i循環(huán)
- j循環(huán)
- 打印"event = [測試事件監(jiān)聽]"
代碼: async
springboot進(jìn)行事件監(jiān)聽有四種方式:
1.手工向ApplicationContext中添加監(jiān)聽器
2.將監(jiān)聽器裝載入spring容器
3.在application.properties中配置監(jiān)聽器
4.通過@EventListener注解實(shí)現(xiàn)事件監(jiā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)
方式1.
首先創(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()); } }
方式2.
創(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())); } }
方式3.
首先創(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
方式4.
創(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())); } }
自定義事件代碼如下:
@SuppressWarnings("serial") public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } }
進(jìn)行測試(在啟動(dòng)類中加入發(fā)布事件的邏輯):
@SpringBootApplication public class LisenterApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args); //裝載事件 context.addApplicationListener(new MyListener1()); //發(fā)布事件 context.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)聽是有序的
完整的代碼路徑:https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot Application事件監(jiān)聽的實(shí)現(xiàn)方案
- springboot+redis過期事件監(jiān)聽實(shí)現(xiàn)過程解析
- SpringBoot加載應(yīng)用事件監(jiān)聽器代碼實(shí)例
- springboot?事件監(jiān)聽器的案例詳解
- SpringBoot利用切面注解及反射實(shí)現(xiàn)事件監(jiān)聽功能
- SpringBoot ApplicationListener事件監(jiān)聽接口使用問題探究
- SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解
- Springboot事件監(jiān)聽與@Async注解詳解
- Java?Springboot異步執(zhí)行事件監(jiān)聽和處理實(shí)例
- SpringBoot實(shí)現(xiàn)事件監(jiān)聽(異步執(zhí)行)的示例代碼
相關(guān)文章
mybatis中使用CASE?WHEN關(guān)鍵字報(bào)錯(cuò)及解決
這篇文章主要介紹了mybatis中使用CASE?WHEN關(guān)鍵字報(bào)錯(cuò)及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12java實(shí)體類轉(zhuǎn)成map的實(shí)現(xiàn)
這篇文章主要介紹了java實(shí)體類轉(zhuǎn)成map的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06springboot整合minio實(shí)現(xiàn)文件存儲(chǔ)功能
MinIO?是一個(gè)基于Apache?License?v2.0開源協(xié)議的對(duì)象存儲(chǔ)服務(wù),它兼容亞馬遜S3云存儲(chǔ)服務(wù)接口,非常適合于存儲(chǔ)大容量非結(jié)構(gòu)化的數(shù)據(jù),本文給大家介紹了springboot整合minio實(shí)現(xiàn)文件存儲(chǔ)功能,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12一問詳解SpringBoot配置文件優(yōu)先級(jí)
在SpringBoot項(xiàng)目當(dāng)中,我們要想配置一個(gè)屬性,可以通過這三種方式當(dāng)中的任意一種來配置都可以,那么優(yōu)先級(jí)怎么算,本文主要介紹了一問詳解SpringBoot配置文件優(yōu)先級(jí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04java web服務(wù)器實(shí)現(xiàn)跨域訪問
這篇文章主要為大家詳細(xì)介紹了java web服務(wù)器實(shí)現(xiàn)跨域訪問,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08IDEA提示:Boolean method ‘xxx‘ is always&nb
這篇文章主要介紹了IDEA提示:Boolean method ‘xxx‘ is always inverted問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08idea報(bào)錯(cuò):程序包org.springframework.web.bind.annotation不存在
在用本地的maven倉庫的時(shí)候會(huì)org.springframework.web.bind.annotation不存在的錯(cuò)誤,本文就詳細(xì)的介紹一下解決方法,感興趣的可以了解下2023-08-08