SpringBoot事件发布和监听详解

作者:ForeverKobe 时间:2022-04-18 02:39:59 

目录
  • 概述

  • 事件监听的结构

    • Publisher,Event和Listener的关系

    • 事件

    • 发布者

    • 监听者

  • 总结

    概述

    ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现,内部实现原理是观察者设计模式,设计初衷也是为了系统业务逻辑之间的解耦,提高可扩展性以及可维护性。事件发布者并不需要考虑谁去监听,监听具体的实现内容是什么,发布者的工作只是为了发布事件而已。事件监听的作用与消息队列有一点类似。

    SpringBoot事件发布和监听详解

    事件监听的结构

    主要有三个部分组成:

    1. 发布者Publisher

    2. 事件Event

    3. 监听者Listener

    SpringBoot事件发布和监听详解

    Publisher,Event和Listener的关系

    SpringBoot事件发布和监听详解

    事件

    我们自定义事件MyTestEvent继承了ApplicationEvent,继承后必须重载构造函数,构造函数的参数可以任意指定,其中source参数指的是发生事件的对象,一般我们在发布事件时使用的是this关键字代替本类对象,而user参数是我们自定义的注册用户对象,该对象可以在监听内被获取。


    @Getter
    public class MyTestEvent extends ApplicationEvent {
       private static final long serialVersionUID = 1L;
       private User user;

    public MyTestEvent(Object source, User user) {
           super(source);
           this.user = user;
       }
    }

    SpringBoot事件发布和监听详解

    发布者

    事件发布是由ApplicationContext对象管控的,我们发布事件前需要注入ApplicationContext对象调用publishEvent方法完成事件发布。

    ApplicationEventPublisher applicationEventPublisher 虽然声明的是ApplicationEventPublisher,但是实际注入的是applicationContext


    @RestController
    @RequestMapping("/test")
    public class TestController {
       @Autowired
       ApplicationContext applicationContext;
       @Autowired
       ApplicationEventPublisher applicationEventPublisher;

    @GetMapping("testEvent")
       public void test() {
           applicationEventPublisher.publishEvent(new MyTestEvent("dzf-casfd-111", new User("dzf-625096527-111", "xiaoming", 19)));
           applicationEventPublisher.publishEvent(new MyTestEvent("dzf-49687489-111", new User("dzf-625096527-111", "xiaowang", 20)));
       }

    }

    监听者

    面向接口编程,实现ApplicationListener接口


    @Component
    public class MyTestListener implements ApplicationListener<MyTestEvent> {

    @Override
       public void onApplicationEvent(MyTestEvent myTestEvent) {
           System.out.println("MyTestListener : " + myTestEvent.getUser());
       }
    }

    使用@EventListener注解配置


    @Component
    public class MyTestListener2{

    @EventListener(MyTestEvent.class)
       public void onApplicationEvent(MyTestEvent myTestEvent) {
           System.out.println("MyTestListener2:" + myTestEvent.getUser());
       }
    }

    来源:https://juejin.cn/post/7028735647389057032

    标签:springboot,事件发布,监听
    0
    投稿

    猜你喜欢

  • Java读取并下载网络文件的方法

    2023-03-18 11:47:05
  • 详解C#中HashTable的用法

    2023-07-17 04:42:07
  • Java ConcurrentHashMap的源码分析详解

    2023-05-02 02:16:21
  • Spring Boot 配置和使用多线程池的实现

    2022-09-04 19:53:02
  • 全面解析Java中的引用类型

    2022-07-15 23:48:07
  • Kotlin语言编程Regex正则表达式实例详解

    2023-06-22 02:06:29
  • 因Spring AOP导致@Autowired依赖注入失败的解决方法

    2022-10-24 19:44:11
  • ConcurrentHashMap是如何实现线程安全的你知道吗

    2023-11-28 23:14:25
  • Java根据模板导出Excel报表并复制模板生成多个Sheet页

    2022-09-09 01:16:01
  • java启动线程的3种方式对比分析

    2023-12-17 08:38:41
  • Java Springboot整合支付宝接口的教程详解

    2023-11-06 19:41:49
  • Java 调用天气Webservice详解及实例代码

    2021-10-09 21:59:03
  • Java读取json数据并存入数据库的操作代码

    2023-09-23 06:00:57
  • C#通过PInvoke调用c++函数的备忘录的实例详解

    2023-11-25 12:53:08
  • Android切换至SurfaceView时闪屏(黑屏闪一下)以及黑屏移动问题的解决方法

    2023-07-21 10:51:45
  • 详解Flutter网络图片本地缓存的实现

    2023-08-18 19:44:43
  • springboot中使用@Transactional注解事物不生效的坑

    2021-10-03 10:01:47
  • SpringBoot默认使用HikariDataSource数据源方式

    2022-03-18 01:04:46
  • java面试题之try中含return语句时代码的执行顺序详解

    2023-11-24 07:34:16
  • 一起来学习C#的观察者模式

    2022-04-02 13:43:15
  • asp之家 软件编程 m.aspxhome.com