Spring应用事件(Application Event)
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)的更多相关文章
- Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)
一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...
- 精通SpringBoot--Spring事件 Application Event
Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Be ...
- spring 事件(Application Event)
spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...
- CL_GUI_ALV_GRID 触发PAI事件(Application event)
*&---------------------------------------------------------------------* *& Report Z_BARRY_A ...
- 从命令模式的维度理解Spring 之Application Event
Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...
- spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持
spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件. spring的 ...
- Spring 事件:Application Event
Spring Application Event Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后, ...
- Spring Application Event Example
Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...
- SpringBoot -- 事件(Application Event)
Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...
随机推荐
- mysql数据库之单表查询
单标查询 单表查询语句 关键字执行的优先级 简单查询 where约束 group by 聚合函数 HAVING过滤 order by 查询排序 LIMIT限制查询的记录数 使用正则表达式查询 单表查询 ...
- Android Studio(十一):代码混淆及打包apk
Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...
- SuperSocket从服务器端主动发起连接
你可以从服务器端主动连接客户端, 连接建立之后的网络通信处理将和客户端主动建立连接的处理方式一样. var activeConnector = appServer as IActiveConnecto ...
- 2018-6-24-WPF-使用RPC调用其他进程
title author date CreateTime categories WPF 使用RPC调用其他进程 lindexi 2018-06-24 14:41:29 +0800 2018-2-13 ...
- [转]什么是CNN、RNN、LSTM
. 全连层 每个神经元输入: 每个神经元输出: (通过一个激活函数) 2. RNN(Recurrent Neural Network) 与传统的神经网络不通,RNN与时间有关. 3. LSTM(Lon ...
- axios 跨域
{ headers:{"Content-Type":"application/x-www-form-urlencoded;charset=utf-8"} ...
- js基础——继承
1.实现继承:原型链 function extend1() {//父类型 this.name = "张三"; } ...
- springboot2.x整合JPA
项目结构 pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...
- H3C 链路聚合显示及维护
- C# 如何给 ValueTuple 返回值添加注释
在 C# 7.0 可以在一个方法的返回,返回多个参数,通过 ValueTuple 的方法,但是和单个参数返回不同的是,如何对多个参数返回每个参数进行单独的注释? 如使用下面的代码,我尝试在一个方法返回 ...