Spring Application Event

Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持。当一个 Bean 处理完一个任务之后,希望另一个 Bean 知道并能做相应的处理,这时我们就需要让另一个 Bean 监听当前 Bean 所发送的事件。(观察者模式)

Spring 的事件需要遵循以下流程:

  • 自定义事件,集成 ApplicationEvent。

  • 定义事件监听器,实现 ApplicationListener。

  • 使用容器发布事件。

以下代码基于 Spring Boot 实现
  1. 自定义事件

    public class DemoEvent extends ApplicationEvent {
    
      private static final long serialVersionUID = 1L;
    private String msg; public DemoEvnet(Object source, String msg) {
    super(source);
    this.msg = msg;
    } public String getMsg() {
    return msg;
    } public void setMsg(String msg) {
    this.msg = msg;
    }
    }
  2. 事件监听者

    @Component
    public class DemoListener implements ApplicationListener<DemoEvent> { public void onApplicationEvent(DemoEvent event) {
    String msg = event.getMsg();
    System.out.println("接收到了消息:" + msg);
    }
    }

代码解释:

实现 ApplicaionListener 接口,并制定监听的时间类型。

使用 onApplicationEvent 方法对消息进行接收处理。

  1. 事件发布者

    @Component
    public class DemoPublisher { @Autowired
    ApplicationContext applicationContext; public void publish(String msg) {
    applicaionContext.publishEvent(new DemoEvent(this, msg));
    }
    }

代码解释:

注入 ApplicaionContext 用来发布事件。

使用 ApplicaionContext 的 publishEvent 方法来发布。

Spring 事件:Application Event的更多相关文章

  1. spring 事件(Application Event)

    spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...

  2. 从命令模式的维度理解Spring 之Application Event

    Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...

  3. Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)

    一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...

  4. 精通SpringBoot--Spring事件 Application Event

    Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Be ...

  5. CL_GUI_ALV_GRID 触发PAI事件(Application event)

    *&---------------------------------------------------------------------* *& Report Z_BARRY_A ...

  6. spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持

    spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件. spring的 ...

  7. Spring Application Event Example

    Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...

  8. SpringBoot -- 事件(Application Event)

    Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...

  9. spring发布和接收定制的事件(spring事件传播)

    spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报  分类: 开源技术(如Struts/spring/Hibernat ...

随机推荐

  1. docker操作

    Redis docker run -itd --name myredis -v /dockerdata/redis/config/redis.conf:/etc/redis/redis.conf  - ...

  2. 2013 ACM/ICPC Asia Regional Online —— Warmup2 ABEGKL

    HDU4716 A. A Computer Graphics Problem A题目描述 题意:输出手机剩余电量,保证给出的数是10的倍数. 题解:水题,按题意输出即可. 代码: #include & ...

  3. 深入理解协程(二):yield from实现异步协程

    原创不易,转载请联系作者 深入理解协程分为三部分进行讲解: 协程的引入 yield from实现异步协程 async/await实现异步协程 本篇为深入理解协程系列文章的第二篇. yield from ...

  4. 【转】基于ArcGIS for javascript api 轨迹回放

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  5. luogu 题解 P2380 【狗哥采矿】

    拿到dp题我们就要想如何推方程 “最北边有bloggium的收集站,最西边有 yeyenum 的收集站.现在要你在这些格子上面安装向北或者向西的传送带(每个格子只能装一种).” 这说明了什么,对于某一 ...

  6. Microsoft Azure Storage Explorer(2)

    之前写过一个往Microsoft Azure Storage Explorer里存储的功能,现在又要把东西给下载下来. 记录一下: public string DownFileFromAzure() ...

  7. http GET 和 POST 请求的优缺点和误区 --前端优化

    Get和Post在面试中一般都会问到,一般的区别:(1)post更安全(不会作为url的一部分,不会被缓存.保存在服务器日志.以及浏览器浏览记录中)(2)post发送的数据更大(get有url长度限制 ...

  8. python的logging模块使用方法

    logging模块 logging模块是Python内置的日志模块,用来生成程序的日志.一条日志对应一个事件的发生,一个事件一般包括:事件发生时间.事件发生位置.事件内容.事件严重程度-日志级别.(还 ...

  9. 投影方式- Unity3D游戏开发培训

    投影方式- Unity3D游戏开发培训   作者:Jesai 2018-02-12 20:33:13 摘  要 透视投影是3D渲染的基本概念,也是3D程序设计的基础.掌握透视投影的原理对于深入理解其他 ...

  10. LeetCode 第七题--整数反转

    1. 题目 2.思路 1. 题目 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321示例 ...