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

  Spring中使用事件的大概流程如下:

  (1)定义事件

  (2)定义事件监听器

  (3)使用容器发布事件


示例:

  (1)定义事件

  自定义事件需要实现ApplicationEvent接口。


package learnspring.learnspring.event;

import org.springframework.context.ApplicationEvent;

/**
* @author 肖政宇
* @date 2019-09-23 14:36
* 说明:自定义事件
*/
public class DemoEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
private String msg; DemoEvent(Object source, String msg) {
super(source);
this.msg = msg;
} String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}

  (2)定义事件监听器

  


package learnspring.learnspring.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* @author 肖政宇
* @date 2019-09-23 14:39
* 说明:事件监听器
* 实现ApplicationListener接口,同时声明监听的事件类型
*/
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {
@Override
public void onApplicationEvent(DemoEvent event) {
String msg = event.getMsg();
System.out.println("我(bean-demoListener)接收到了bean-demoPublisher发布的消息:" + msg);
}
}
 

  (3)使用容器发布事件

package learnspring.learnspring.event;
package learnspring.learnspring.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; /**
* @author 肖政宇
* @date 2019-09-23 14:43
* 说明:事件发布者
*/
@Component
public class DemoPublisher {
private ApplicationContext applicationContext; @Autowired
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
} public void publish(String msg) {
applicationContext.publishEvent(new DemoEvent(this, msg));
}
}

测试:


package learnspring.learnspring;

import learnspring.learnspring.event.DemoPublisher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class LearnspringApplicationTests {
DemoPublisher publisher; @Autowired
public void setPublisher(DemoPublisher publisher) {
this.publisher = publisher;
} @Test
public void contextLoads() {
System.out.println("事件发生!");
publisher.publish("Hello World!");
} }
 

运行结果:

  

Spring应用事件(Application Event)的更多相关文章

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

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

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

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

  3. spring 事件(Application Event)

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

  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. Spring Application Event Example

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

  9. SpringBoot -- 事件(Application Event)

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

随机推荐

  1. 4818 Largest Empty Circle on a Segment (几何+二分)

    ACM-ICPC Live Archive 挺水的一道题,直接二分圆的半径即可.1y~ 类似于以前半平面交求核的做法,假设半径已经知道,我们只需要求出线段周围哪些位置是不能放置圆心的即可.这样就转换为 ...

  2. 第三期 预测——Frenet 坐标

    Frenet坐标 在讨论过程模型之前,我们应该提到“Frenet Coordinates”,它是一种以比传统x,y笛卡尔坐标更直观的方式表示道路位置的方式. 用Frenet坐标,我们使用变量 s和d描 ...

  3. 各种浏览器怎么换ip

    https://jingyan.baidu.com/article/e4d08ffdb784050fd2f60ddd.html 方法/步骤   1 用浏览器搜索ip,得到自己当前的ip. 2 用浏览器 ...

  4. JavaScript 全国级省市县联动

    <div class="right_content clearfix"> <h3 class="common_title2">收货地址& ...

  5. Python--day67--CBV和FBV、Request对象及上传文件示例

    1,CBV版添加新的出版社 views.py文件 urls.py文件 2,Request对象: request对象 当一个页面被请求时,Django就会创建一个包含本次请求原信息的HttpReques ...

  6. Python--day19--random模块

    random模块 >>> import random #随机小数 >>> random.random() # 大于0且小于1之间的小数 0.766433866365 ...

  7. 符合阿里巴巴代码规范的checkstyle检测文件

    一.安装与简介 eclipse和idea都有对应的插件,找到插件安装界面.搜索checkstyle,点击安装后,重启IDE即可.(网上有很多安装教程,就不重复制造轮子了) 二.导入配置文件 在chec ...

  8. python类中的双下划线方法

    __getitem__,__setitem__和__delitem__ 实现了对象属性的字典化操作. class Person: def __init__(self, name, age, hobby ...

  9. java 反射和泛型-反射来获取泛型信息

    通过指定对应的Class对象,程序可以获得该类里面所有的Field,不管该Field使用private 方法public.获得Field对象后都可以使用getType()来获取其类型. Class&l ...

  10. vue 路由跳转前确认框,刷新浏览器页面前提示确认框

    先看效果图: 1.刷新页面效果: 2.跳转路由(进入别的页面前)效果: 代码: // 路由跳转确认 beforeRouteLeave(to, from, next) { const answer = ...