10点睛Spring4.1-Application Event
10.1 Application Event
- Spring使用Application Event给bean之间的消息通讯提供了手段
- 应按照如下部分实现bean之间的消息通讯
- 继承ApplicationEvent类实现自己的事件
- 实现继承ApplicationListener接口实现监听事件
- 使用ApplicationContext发布消息
10.2示例
示例中的通讯两个bean分别为DemoListener和Main
10.2.1 编写自定义的Application Event
package com.wisely.event;
import org.springframework.context.ApplicationEvent;
public class DemoEvent extends ApplicationEvent{
private static final long serialVersionUID = 1L;
private String msg;
public DemoEvent(Object source,String msg) {
super(source);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
10.2.2 编写实现ApplicationListener的类
package com.wisely.event; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class DemoListener implements ApplicationListener<DemoEvent> { public void onApplicationEvent(DemoEvent event) {
String msg = ((DemoEvent) event).getMsg();
System.out.println("我监听到了pulisher发布的message为:"+msg); } }
10.2.3 测试
package com.wisely.event; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Main { public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.event");
Main main =context.getBean(Main.class);
main.pulish(context);
context.close(); }
public void pulish(AnnotationConfigApplicationContext context){
context.publishEvent(new DemoEvent(this, "22"));
} }
输出结果
我监听到了pulisher发布的message为:22
10点睛Spring4.1-Application Event的更多相关文章
- Spring Application Event Example
Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...
- Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)
一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...
- 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的 ...
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- Spring 事件:Application Event
Spring Application Event Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后, ...
- 从命令模式的维度理解Spring 之Application Event
Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
随机推荐
- Hive 调优
今天总结本人在使用Hive过程中的一些优化技巧,希望给大家带来帮助.Hive优化最体现程序员的技术能力,面试官在面试时最喜欢问的就是Hive的优化技巧. 技巧1.控制reducer数量 下面的内容是我 ...
- Spring入门(二)——DI
1. DI Dependency Injection,依赖注入.当对象里有属性或对象的时候,就需要为这些属性或对象赋值 2. 流程 这里介绍两种方式 set方法 注解方式 2.1 set方法 Bean ...
- QtWebEngineWidgets
我用的qt5.10+VS2017,2013应该一样项目属性里手动添加包含目录:(QTDIR)\include\QtWebEngineWidgets,(QTDIR)\include\QtWebChann ...
- 布鲁克斯法则 (Brooks's Law)
软件开发后期,添加人力只会使项目开发得更慢. 这个定律表明,在许多情况下,试图通过增加人力来加速延期项目的交付,将会使项目交付得更晚.布鲁克斯也明白,这是一种过度简化.但一般的推理是,新资源的增加时间 ...
- matlab 万能实用的非线性曲线拟合方法
——转载网络 在科学计算和工程应用中,经常会遇到需要拟合一系列的离散数据,最近找了很多相关的文章方法,在这里进行总结一下其中最完整.几乎能解决所有离散参数非线性拟合的方法 第一步:得到散点数据 根据你 ...
- Codeforces 1137F Matches Are Not a Child's Play [LCT]
Codeforces 很好,通过这题对LCT的理解又深了一层. 思路 (有人说这是套路题,然而我没有见过/kk) 首先发现,删点可以从根那里往下删,非常难受,所以把权值最大的点提为根. 然后考虑\(x ...
- 【洛谷】P3177 [HAOI2015]树上染色
懒得复制题面了直接传送门吧 分析 直接求点与点之间的距离感觉不是很好求,所以我们考虑换一个求法. 瞄了一眼题解 距离跟路径上边的长度有关,所以我们直接来看每一条边的贡献吧(这谁想得到啊) 对于每一条边 ...
- 第07组 Alpha冲刺(1/6)
队长:杨明哲 组长博客:求戳 作业博客:求再戳 队长:杨明哲 过去两天完成了哪些任务 文字/口头描述:完成了,网页后端的大部分工作.负责了很大一部分的后端工作. 展示GitHub当日代码/文档签入记录 ...
- vue+elementui搭建后台管理界面(5递归生成侧栏路由)
有一个菜单树,顶层菜单下面有多个子菜单,子菜单下还有子菜单... 这时候就要用递归处理 1 定义多级菜单 修改 src/router/index.js 的 / 路由 { path: '/', redi ...
- php中函数 isset(), empty(), is_null() 的区别
NULL:当你在你的脚本中写下这样一行代码 $myvariable; //此处你想定义一个变量,但未赋值.会有Notice: Undefined variable echo $myvariable + ...