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)器類的實(shí)現(xiàn)方式:
- 自定義事件:繼承自ApplicationEvent抽象類,然后定義自己的構(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類
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)類中獲取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類,并使用@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)聽(tīng)到事件源:%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)聽(tīng)到事件源:%s.", MyListener3.class.getName(), event.getSource()));
}
}
然后在application.properties中配置監(jiān)聽(tīng)
context.listener.classes=com.listener.MyListener3
方式4.
創(chuàng)建MyListener4類,該類無(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)類中加入發(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)文章
Java中Mybatis,SpringMVC,Spring的介紹及聯(lián)系
這篇文章主要為大家詳細(xì)介紹了Java中Mybatis,SpringMVC,Spring的介紹及聯(lián)系,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
Nacos docker單機(jī)模式部署實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Nacos docker單機(jī)模式部署實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
springboot內(nèi)置的tomcat支持最大的并發(fā)量問(wèn)題
這篇文章主要介紹了springboot內(nèi)置的tomcat支持最大的并發(fā)量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
JDK9對(duì)String字符串的新一輪優(yōu)化
這篇文章主要介紹了JDK9對(duì)String字符串的新一輪優(yōu)化,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Springboot下RedisTemplate的兩種序列化方式實(shí)例詳解
這篇文章主要介紹了Springboot下RedisTemplate的兩種序列化方式,通過(guò)定義一個(gè)配置類,自定義RedisTemplate的序列化方式,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09

