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

