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

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

  1. 自定义事件,集成ApplicationEvent。
  2. 定义事件监听器,实现ApplicationListener。
  3. 使用容器发布事件。

一、自定义事件

  1. package com.cenobitor.appevent.event;
  2.  
  3. import org.springframework.context.ApplicationEvent;
  4.  
  5. public class DemoEvent extends ApplicationEvent {
  6.  
  7. private static final long serialVersionUID = 1L;
  8. private String msg;
  9.  
  10. public DemoEvent(Object source,String msg) {
  11. super(source);
  12. this.msg = msg;
  13. }
  14.  
  15. public String getMsg() {
  16. return msg;
  17. }
  18.  
  19. public void setMsg(String msg) {
  20. this.msg = msg;
  21. }
  22. }

二、事件监听器

  1. package com.cenobitor.appevent.listener;
  2.  
  3. import com.cenobitor.appevent.event.DemoEvent;
  4. import org.springframework.context.ApplicationListener;
  5. import org.springframework.stereotype.Component;

  6. //实现ApplicationListener接口,并指定监听的事件类型
  7. @Component
  8. public class DemoListener implements ApplicationListener<DemoEvent> {
  9.  
  10. @Override
      //使用onApplicationEvent方法对消息进行接收处理
  11. public void onApplicationEvent(DemoEvent demoEvent) {
  12. String msg = demoEvent.getMsg();
  13. System.out.println("(bean-demoListener)接收到了bean-demoPublisher发布的消息:"+msg);
  14. }
  15. }

三、事件发布类

  1. package com.cenobitor.appevent.publisher;
  2.  
  3. import com.cenobitor.appevent.event.DemoEvent;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.stereotype.Component;
  7.  
  8. @Component
  9. public class DemoPublisher {
  10. @Autowired
      //注入ApplicationContext用来发布事件
  11. ApplicationContext applicationContext;
  12.   //使用ApplicationContext的publishEvent方法来发布
  13. public void publish(String msg){
  14. applicationContext.publishEvent(new DemoEvent(this,msg));
  15. }
  16. }

四、运行

  1. package com.cenobitor.appevent;
  2.  
  3. import com.cenobitor.appevent.publisher.DemoPublisher;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5.  
  6. public class Main {
  7. public static void main(String[] args) {
  8. AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppeventApplication.class);
  9. DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
  10. demoPublisher.publish("hello application event");
  11. context.close();
  12. }
  13.  
  14. }

结果:(bean-demoListener)接收到了bean-demoPublisher发布的消息:hello application event

注:摘抄自《JavaEE开发的颠覆者SpringBoot 实战》。

SpringBoot -- 事件(Application Event)的更多相关文章

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

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

  2. spring 事件(Application Event)

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

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

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

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

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

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

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

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

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

  7. Spring 事件:Application Event

    Spring Application Event Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后, ...

  8. SpringBoot事件监听器源码分析

    本文涉及到Spring的监听器,如果不太了解请先阅读之前的Spring监听器的文章. SpringBoot事件监听器初始化 SpringBoot中默认定义了11个事件监听器对象,全部定义在META-I ...

  9. Spring Application Event Example

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

随机推荐

  1. IO模型 IO多路复用

    阻塞IO 用socket 一定会用到accept recv recvfrom这些方法正常情况下 accept recv recvfrom都是阻塞的 非阻塞IO 如果setblocking(False) ...

  2. 522. Longest Uncommon Subsequence II

    Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...

  3. Linux系统文件权限管理(6)

    Linux操作系统是多任务(Multi-tasks)多用户(Multi-users)分时操作系统,linux操作系统的用户就是让我们登录到linux的权限,每当我们使用用户名登录操作系统时,linux ...

  4. MariaDB 插入&更新&删除数据(8)

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...

  5. CSP攻略

    看完三篇文章应该就懂了csp是干嘛的. https://www.cnblogs.com/Wayou/p/intro_to_content_security_policy.html https://ww ...

  6. POJ 2643

    #include<iostream> #include<stdio.h> #include<string> #include<algorithm> #d ...

  7. nginx lua集成

    版本说明: linux: 1.8.1 luajit:2.0.2(不要使用标准lua,应当使用luajit, 后者的效率比前者高很多) ngx_devel_kit: 0.2.18 lua-nginx-m ...

  8. 工具-CrashMonkey4IOS,Monkey测试方案

    在TesterHome看到了CrashMonkey4IOS,顿时觉得之前用instrument在做monkey测试,非常的弱智!crash后啥都看不到,无crashlog,无crash步骤,并且也不能 ...

  9. [Java初探实例篇02]__流程控制语句知识相关的实例练习

    本例就流程控制语句的应用方面,通过三个练习题来深入学习和巩固下学习的流程控制语句方面的知识,设计到,if条件判断语句,switch多分支语句,for循环语句及其嵌套多层使用,while循环语句. 练习 ...

  10. Ceph 块设备 - 块设备快速入门

    目录 一.准备工作 二.安装 Ceph 三.使用块存储   一.准备工作 本文描述如何安装 ceph 客户端,使用 Ceph 块设备 创建文件系统并挂载使用. 必须先完成 ceph 存储集群的搭建,并 ...