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. 快速掌握—HTML快速实现自定义Input开关

    HTML <input id="customSwitch" type="checkbox" /> <label for="custo ...

  2. 【转】7本免费的Java电子书和教程

    本文由 ImportNew - 唐小娟 翻译自 Javapapers.如需转载本文,请先参见文章末尾处的转载要求. 1. Thinking in Java (Third Edition) 本书的作者是 ...

  3. GeneXus DevOps 自动化构建和部署流程

    以下视频详细介绍了GeneXus DevOps自动化构建和部署流程,包括通过MS Bulid来管理自动化流程,自动化的架构,以及在GeneXus Server上使用Jenkins做为自动化引擎. 视频 ...

  4. cogs 182. [USACO Jan07] 均衡队形 线段树

    182. [USACO Jan07] 均衡队形 ★★☆   输入文件:lineup.in   输出文件:lineup.out   简单对比时间限制:4 s   内存限制:128 MB 题目描述 农夫约 ...

  5. 使用git将本地文件提交到github存储库

    1.首先你要安装git https://git-for-windows.github.io/ 去官网自行下载对应版本 2.安装好git服务器后,找到你项目的文件夹,右键git bash here打开命 ...

  6. Docker 学习 1 入门

    Docker 学习 1 入门 dockert 安装. Mac Ubuntu 查看docker 版本 docker version 拉取image. docker pull e.g docker pul ...

  7. Asp.Net Core Identity 隐私数据保护

    前言 Asp.Net Core Identity 是 Asp.Net Core 的重要组成部分,他为 Asp.Net Core 甚至其他 .Net Core 应用程序提供了一个简单易用且易于扩展的基础 ...

  8. 「 扫盲 」Web服务器和应用服务器的区别

    我们经常使用apache,tomcat,nginx,jetty等服务器,但并不清楚它们间的区别,它们中,哪些是Web服务器,哪些是应用服务器?今天就来告诉你 Web服务器 理解WEB服务器,首先你要理 ...

  9. Kindle Unlimited 上线的最热书单

    Kindle 也给出了一份,到现在为止,在 Kindle Unlimited 上线的最热书单: 1.岛上书店2.一个人的朝圣3.自控力4.嫌疑人 X 的献身5.沉默的大多数(王小波文集)6.跟任何人都 ...

  10. 理解Javascript的柯里化

    前言 本文1454字,阅读大约需要4分钟. 总括: 本文以初学者的角度来阐述Javascript中柯里化的概念以及如何在工作中进行使用. 原文地址:理解Javascript的柯里化 知乎专栏: 前端进 ...