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
相关推荐
国元点睛-20230825-国元期货-23页
眼睛穴位图-睛明穴-取穴方法 方便经常上网的朋友使用
六爱护眼睛---复式统计图(第1课时).doc
图像与眼睛(2009-04-1.pdf
让消费者眼睛亮起来--稀世宝矿泉水整合营销策划案例(doc 20).doc
包含了谷歌眼睛的全部核心代码,可以用记事本、Dreamweaver、写字板、html等打开
这是一款保护眼睛的小软件,绿色软件,不需安装即可使用,试一下吧。
[最新]八年级上册物理:5.4-眼睛和眼镜-精品教案.pdf
闭着眼睛记单词-英语基础《英语1000高频词汇》
event detection with deep neural networks}, author={Zemblys, Raimondas and Niehorster, Diederick C and Holmqvist, Kenneth}, journal={Behavior research methods}, year={2018}, } GazeNet 是在 Linux 上...
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
使用OpenCV1.0实现了眼睛状态识别,首先用harr检测眼睛,然后用模板匹配的方法标记眼睛状态。 程序需要使用Eyes45x11.xml Mouth25x15.xml Nose25x15.xml,将其放到C:\Program Files\OpenCV\data\haarcascades目录下...
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
第一部分 点睛Spring 4.x 第1 章 Spring 基础 2 1.1 Spring 概述 2 1.1.1 Spring 的简史 2 1.1.2 Spring 概述 3 1.2 Spring 项目快速搭建 5 1.2.1 Maven 简介 6 1.2.2 Maven 安装 6 1.2.3 Maven 的pom.xml 7 1.2.4 ...
opencv用于检测带眼镜的眼睛haarcascade_eye_tree_eyeglasses.xml
opencv用于检测眼睛。haarcascade_eye.xml文件
上网保护眼睛--网页底色修改.2011-11-25最新整理,2011-11-25最新整理...
点睛-文本各种编码方式转变.7z
软件名:点睛文本编码查询 功能:本软件用于将文本的字符串转换为 Text、ASCII、Default、Unicode、BigEndianUnicode、UTF-8、UTF-7、GBK、BIG5 之间的互相转换,用于文本查询。转换结果使用十六进制表示。