springboot 事件监听的实现方法
- 更新时间:2023-01-11 17:09:09
- 编辑:韶翰学
参考资料
- 《基于SpringBoot实现:Java分布式中间件开发入门与实战》源码文件 配套资源 / 206 MB / 钟林森 推荐度:
- SpringBoot揭秘:快速构建微服务体系 PDF 电子书 / 29.5 MB / 王福强 推荐度:
- SpringBoot全教程 / 20.6 MB / 码小辫 推荐度:
正文内容
定义事件
@Getter public class TestEvent extends ApplicationEvent { private String msg; public TestEvent(Object source, String msg) { super(source); this.msg = msg; } }
定义事件监听(注解方式)
@Component public class TestListen { @EventListener public void testListen(TestEvent event) { System.out.println(event.getMsg()); } }
注意:@Component 注解
发布事件
@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, "测试事件监听")); for (int j = 0; j < 10; j++) { System.out.println("j = " + j); } }
测试时执行顺序:
- i循环
- 打印"event = [测试事件监听]"
- j循环
异步监听
监听加上@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() + "]"); } } }
测试时执行顺序:
- i循环
- j循环
- 打印"event = [测试事件监听]"
代码: async
springboot进行事件监听有四种方式:
1.手工向ApplicationContext中添加监听器
2.将监听器装载入spring容器
3.在application.properties中配置监听器
4.通过@EventListener注解实现事件监听
讲到事件监听,这里我们说下自定义事件和自定义监听器类的实现方式:
- 自定义事件:继承自ApplicationEvent抽象类,然后定义自己的构造器
- 自定义监听:实现ApplicationListener<T>接口,然后实现onApplicationEvent方法
下面讲下4种事件监听的具体实现
方式1.
首先创建MyListener1类
public class MyListener1 implements ApplicationListener<MyEvent> { Logger logger = Logger.getLogger(MyListener1.class); public void onApplicationEvent(MyEvent event) { logger.info(String.format("%s监听到事件源:%s.", MyListener1.class.getName(), event.getSource())); } }
然后在springboot应用启动类中获取ConfigurableApplicationContext上下文,装载监听
@SpringBootApplication public class LisenterApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args); //装载监听 context.addApplicationListener(new MyListener1()); } }
方式2.
创建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监听到事件源:%s.", MyListener2.class.getName(), event.getSource())); } }
方式3.
首先创建MyListener3类
public class MyListener3 implements ApplicationListener<MyEvent> { Logger logger = Logger.getLogger(MyListener3.class); public void onApplicationEvent(MyEvent event) { logger.info(String.format("%s监听到事件源:%s.", MyListener3.class.getName(), event.getSource())); } }
然后在application.properties中配置监听
context.listener.classes=com.listener.MyListener3
方式4.
创建MyListener4类,该类无需实现ApplicationListener接口,使用@EventListener装饰具体方法
@Component public class MyListener4 { Logger logger = Logger.getLogger(MyListener4.class); @EventListener public void listener(MyEvent event) { logger.info(String.format("%s监听到事件源:%s.", MyListener4.class.getName(), event.getSource())); } }
自定义事件代码如下:
@SuppressWarnings("serial") public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } }
进行测试(在启动类中加入发布事件的逻辑):
@SpringBootApplication public class LisenterApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args); //装载事件 context.addApplicationListener(new MyListener1()); //发布事件 context.publishEvent(new MyEvent("测试事件.")); } }
启动后,日志打印如下:
2018-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener3 : com.listener.MyListener3监听到事件源:测试事件..
2018-06-15 10:51:20.198 INFO 4628 --- [ main] com.listener.MyListener4 : com.listener.MyListener4监听到事件源:测试事件..
2018-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener2 : com.listener.MyListener2监听到事件源:测试事件..
2018-06-15 10:51:20.199 INFO 4628 --- [ main] com.listener.MyListener1 : com.listener.MyListener1监
听到事件源:测试事件..
由日志打印可以看出,SpringBoot四种事件的实现方式监听是有序的
完整的代码路径:https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农之家。
springboot相关教程
-
SpringBoot+Lucene实例介绍
这篇文章主要介绍了详解SpringBoot+Lucene案例介绍,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
发布时间:2020-07-16
-
SpringBoot实现前端验证码图片生成和校验的实例讲解
这篇文章主要为大家详细介绍了SpringBoot实现前端验证码图片生成和校验,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
发布时间:2019-09-10
-
SpringBoot 整合 Lettuce Redis实例讲解
这篇文章主要介绍了SpringBoot 整合 Lettuce Redis的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
发布时间:2021-04-29
-
SpringBoot实用小技巧之如何动态设置日志级别
给大家整理了关于SpringBoot的教程,这篇文章主要给大家介绍了关于SpringBoot实用小技巧之如何动态设置日志级别的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用SpringBoot具有一定的参考学习价值,需要的朋友
发布时间:2022-09-23
-
SpringBoot如何用Spring-Data-Jpa实现CRUD操作
这篇文章主要为大家详细介绍了SpringBoot使用Spring-Data-Jpa实现CRUD操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
发布时间:2021-05-25
-
springboot集成@DS注解实现数据源切换的方法示例
给大家整理一篇关于springboot的教程,本文主要介绍了springboot集成@DS注解实现数据源切换的方法示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
发布时间:2022-09-14
-
springboot写一个自己的starter源码实例详解
在本篇文章里小编给大家整理了关于springboot手写一个自己的starter源码的全部知识点内容,需要的朋友们学习下。
发布时间:2019-07-30
-
JAVA快速搭建基本的springboot
本文主要入门者介绍怎么搭建一个基础的springboot环境,本文通过图文并茂的形式给大家介绍从spring boot到spring cloud的完美搭建过程,适用java入门教学,需要的朋友可以参考下
发布时间:2021-05-28
-
SpringBoot接口加密解密处理方法详解
这篇文章主要为大家详细介绍了SpringBoot接口加密解密统一处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
发布时间:2020-01-16