Spring----事件(Application Event)
1、概述
1.1、Spring的事件 为Bean与Bean之间的消息通信提供了支持;
当一个Bean处理完一个任务后,希望另一个Bean知道并能做出相应的处理,这时我们需要 让另一个Bean 监听 当前Bean所发送的事件;
1.2、Spring的事件需要遵循如下流程:
a,自定义事件,继承ApplicationEvent
b,定义事件监听器,实现ApplicationListener
c,使用容器发布事件
1.3、eg:
package com.an.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* @description: 事件配置类
* @author: anpeiyong
* @date: Created in 2019/11/19 16:41
* @since:
*/
@Configuration
@ComponentScan(value = "com.an")
public class EventConfig {
}
package com.an.event; import org.springframework.context.ApplicationEvent; /**
* @description: 自定义事件
* @author: anpeiyong
* @date: Created in 2019/11/19 16:32
* @since:
*/
public class MyEvent extends ApplicationEvent { private String msg; public MyEvent(Object source,String msg) {
super(source);
this.msg=msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
package com.an.event; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* @description: 自定义事件监听器
* 实现ApplicationListener接口,并指定监听的事件类型ApplicationListener<MyEvent>
* 使用onApplicationEvent()方法对消息进行接收处理
* @author: anpeiyong
* @date: Created in 2019/11/19 16:34
* @since:
*/
@Component
public class MyListener implements ApplicationListener<MyEvent> { @Override
public void onApplicationEvent(MyEvent event) {
String msg=event.getMsg();
System.out.println("我接收到的消息:"+msg);
}
}
package com.an.event; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/19 16:38
* @since:
*/
@Component
public class MyPublisher { //注入ApplicationContext,用来发布事件
@Autowired
ApplicationContext applicationContext; public void publish(String msg){
//使用ApplicationContext.publishEvent()发布
applicationContext.publishEvent(new MyEvent(this,msg));
}
}
package com.an.main; import com.an.config.EventConfig;
import com.an.event.MyPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/19 16:41
* @since:
*/
public class EventMainTest { public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(EventConfig.class);
MyPublisher myPublisher=annotationConfigApplicationContext.getBean(MyPublisher.class);
myPublisher.publish("hello");
annotationConfigApplicationContext.close();
} }
结果:
我接收到的消息:hello
Spring----事件(Application Event)的更多相关文章
- spring 事件(Application Event)
spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...
- 从命令模式的维度理解Spring 之Application Event
Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...
- Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)
一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...
- 精通SpringBoot--Spring事件 Application Event
Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Be ...
- CL_GUI_ALV_GRID 触发PAI事件(Application event)
*&---------------------------------------------------------------------* *& Report Z_BARRY_A ...
- 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所发送的事件. ...
- spring发布和接收定制的事件(spring事件传播)
spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报 分类: 开源技术(如Struts/spring/Hibernat ...
随机推荐
- NOIp 图论算法专题总结 (1):最短路、最小生成树、最近公共祖先
系列索引: NOIp 图论算法专题总结 (1) NOIp 图论算法专题总结 (2) NOIp 图论算法专题总结 (3) 最短路 Floyd 基本思路:枚举所有点与点的中点,如果从中点走最短,更新两点间 ...
- LintCode之最长单词
题目描述: 分析:先建一个数组s用来存储每个字符串的长度,然后遍历数组s得到最大的数max,这个数就是词典中的最长单词的长度,由于可能有多个长度相等的单词,所以要循环整个词典,当一个单词的长度等于ma ...
- python实现堆栈与队列的方法
python实现堆栈与队列的方法 本文实例讲述了python实现堆栈与队列的方法.分享给大家供大家参考.具体分析如下: 1.python实现堆栈,可先将Stack类写入文件stack.py,在其它程序 ...
- vue.js + element 搭建后台管理系统 笔记(一)
此文仅记录本人在搭建后台系统过程中遇到的难点及注意点,如果能帮到各位自然是极好的~~~ 项目主要架构:vueJS.elementUI.scss 一.项目初始化 首先需要安装nodejs,安装方法就不在 ...
- UCenter 与 DIscuz 通信失败的解决方法
问题状况:Discuz 用户无法成功修改头像且帖子中上传的图片无法保存.进入 Discuz 后台检查,一切正常:进入 UCenter 检查后发现在"应用管理"中与 Discuz 论 ...
- 排序算法一:插入排序(Insertion sort)
最近从网易公开课在看麻省理工学院的公开课<算法导论>,感觉还不错,接下来几篇文章所示学习日记了,不准备对算法细节做过多描述,感兴趣的可以自己去看. 文章分几篇讲经典排序算法,直接上代码,根 ...
- Ubuntu12.04下安装Subversion并进行配置
Ubuntu下安装Subversion还是很简单的,只要输入sudo apt-get install Subversion就可以安装了. 主要的难点在于对权限的配置上. 安装完subversion后, ...
- Flutter 仿滴滴出行App
绿色出行 Flutter 仿滴滴出行App 地图:采用高德地图,仅简单完成了部分功能,基础地图,地址检索,逆地理编码. 界面:仿滴滴主界面,地图中心请求动效果,服务tabs展开效果,地址检索界面,城市 ...
- flask项目中使用富文本编辑器
flask是一个用python编写的轻量级web框架,基于Werkzeug WSGI(WSGI: python的服务器网关接口)工具箱和Jinja2模板,因为它使用简单的核心,用extension增加 ...
- python 模块和包深度学习理解
python 模块和包 简单说相当于命名空间 1,python 模块 python模块就是一个文件,里面有函数,变量等 import 模块 模块.方法 from 模块 import fu ...