精通SpringBoot--Spring事件 Application Event
Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Bean所发送的事件。
要实现事件的监听,我们要做两件事:
1:自定义事件,继承ApplicationEvent接口
2:定义事件监听器,实现ApplicationListener
3:事件发布类
/**
* @TODO // 自定义事件,继承ApplicationEvent接口
* @Author Lensen
* @Date 2018/7/22
* @Description
*/
public class SendMsgEvent extends ApplicationEvent {
private static final long serialVersionID = 1L;
// 收件人
public String receiver;
// 收件内容
public String content;
public SendMsgEvent(Object source) {
super(source);
}
public SendMsgEvent(Object source, String receiver, String content) {
super(source);
this.receiver = receiver;
this.content = content;
}
public void output(){
System.out.println("I had been sand a msg to " + this.receiver);
}
}
/**
* @TODO //定义事件监听器,实现ApplicationListener
* @Author Lensen
* @Date 2018/7/22
* @Description
*/@Component
public class MsgListener implements ApplicationListener<SendMsgEvent> {
@Override
public void onApplicationEvent(SendMsgEvent sendMsgEvent) {
sendMsgEvent.output();
System.out.println(sendMsgEvent.receiver + "received msg : " + sendMsgEvent.content );
}
}
事件发布类
@Component
public class Publisher {
@Autowired
ApplicationContext applicationContext;
public void publish(Object source, String receiver, String content){
applicationContext.publishEvent(new SendMsgEvent(source, receiver, content));
}
}
测试消息:WebConfig.class主要是为了扫描Publisher 和Listener类。里面有两个注解@ComponenScan和@Configuration。
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(WebConfig.class);
Publisher publisher = applicationContext.getBean(Publisher.class);
publisher.publish("Hello,World!","Mr.Lensen", "I Love U");
}
结果:
I had been sand a msg to Mr.Lensen
Mr.Lensen received msg : I Love U
精通SpringBoot--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知道并能做相应的处理,这时我们就需要 ...
- CL_GUI_ALV_GRID 触发PAI事件(Application event)
*&---------------------------------------------------------------------* *& Report Z_BARRY_A ...
- SpringBoot -- 事件(Application Event)
Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...
- 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零XML配置的Spring Boot Application
Spring Boot 提供了一种统一的方式来管理应用的配置,允许开发人员使用属性properties文件.YAML 文件.环境变量和命令行参数来定义优先级不同的配置值.零XML配置的Spring B ...
随机推荐
- Linux网络管理命令ifdown/ifup与ifconfig/ip中的down/up命令的对比
参考了:https://blog.csdn.net/GDUTLYP/article/details/50498202 以下网卡均采用eth1说明. 相同点——[启用]和[禁止]网卡 ifdown et ...
- linux之sed的用法
sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为: sed ...
- GraphQL实战经验和性能问题的解决方案
在现在的公司使用GraphQL有一段时间了. 现公司从创立之后的很长一段时间内是纯PHP的技术栈,前端.后端都在PHP代码中糅合在一起.新功能越加越多,页面越来越复杂之后,那些混在在PHP代码中的HT ...
- 【密码学】MD5算法原理
MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权费用. MD5功能: 输 ...
- opencv——IplImage结构
一.作业要求: 采用MATLAB或opencv+C编程实现.每一题写明题目,给出试验程序代码,实验结果图片命名区分并作出效果比对,最后实验总结说明每一题蕴含的图像处理方法的效果以及应用场合等. 采用M ...
- pat1059. Prime Factors (25)
1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...
- jdbc操作数据库的步骤
package com.jckb; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...
- IDEA/AS快捷键收集&习惯
1.Alt+Enter单包引入 2.Ctrl+O (在类中)快速重写父类方法 3.Ctrl+F12显示类结构 4.代码提示 -Ctrl+Alt+空格 代码提示 -Ctrl+Shift+回车 在末尾自动 ...
- 配置Ubuntu DNS
首先,你可以在/etc/hosts中加入一些主机名称和这些主机名称对应的IP地址,这是简单使用本机的静态查询.要访问Ubuntu DNS 服务器来进行查询,需要设置/etc/resolv.conf文件 ...
- Check-Point-Security-Gateway-BYOL-R77.30-041.161
平台: CentOS 类型: 虚拟机镜像 软件包: checkpoint redhat smartconsole basic software security vfw 服务优惠价: 按服务商许可协议 ...