spring之ApplicationEvent 事件驱动
2024-09-04 00:38:32
什么是ApplicationContext?
它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些。
ApplicationContext则是应用的容器。
Spring把Bean(object)放在容器中,需要用就通过get方法取出来。
ApplicationEvent
是个抽象类,里面只有一个构造函数和一个长整型的timestamp。
ApplicationListener
是一个接口,里面只有一个onApplicationEvent方法。
所以自己的类在实现该接口的时候,要实装该方法。
如果在上下文中部署一个实现了ApplicationListener接口的bean,
那么每当在一个ApplicationEvent发布到 ApplicationContext时,
这个bean得到通知。其实这就是标准的Observer设计模式
首先创建一个Event事件类
public class EmailListEvent extends ApplicationEvent {
2.
3. private static final long serialVersionUID = 1L;
4. public String address;
5. public String text;
6.
7. public EmailListEvent(Object source) {
8. super(source);
9. }
10.
11. public EmailListEvent(Object source, String address, String text) {
12. super(source);
13. this.address = address;
14. this.text = text;
15. }
16.
17. public void print() {
18. System.out.println("Hello,Spring Event!!!");
19. }
20. }
其次创建一个ApplicationListener类:
public class EmailNotifier implements ApplicationListener {
2.
3. public void onApplicationEvent(ApplicationEvent evt) {
4. if (evt instanceof EmailListEvent) {
5. EmailListEvent emailEvent = (EmailListEvent) evt;
6. emailEvent.print();
7. System.out.println("the source is:" + emailEvent.getSource());
8. System.out.println("the address is:" + emailEvent.address);
9. System.out.println("the mail's context is :" + emailEvent.text);
10. }
11.
12. }
接着将Listener注册到Spring的xml文件中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="emailListListener" class="spring.event.EmailNotifier"></bean> </beans>
MAIN
1. public class ListenerEventDemo {
2.
3. /**
4. * @param args
5. */
6. public static void main(String[] args) {
7. ApplicationContext context = new ClassPathXmlApplicationContext(
8. "SpringEvent.xml");
9. EmailListEvent emailListEvent = new EmailListEvent("hello",
10. "helloSpring@sina.com", "this is a test eamil content");
11. //在ApplicationContext中发布一个 ApplicationEvent
12. context.publishEvent(emailListEvent);
13. }
14.
测试结果:
# Hello,Spring Event!!!
# the source is:hello
# the address is:helloSpring@sina.com
# the mail's context is :this is a test eamil content
最新文章
- 嵌入式Linux驱动学习之路(二十五)虚拟网卡驱动程序
- A Simple OpenCASCADE Qt Demo-occQt
- 前端 angular 和 bootstrap 的安装步骤
- SPM12manual,统计部分(8-10)笔记
- 设置sublime text2/3中默认预览浏览器快捷键的方法
- 剑指offer习题集2
- python 把数据 json格式输出
- [ROS]1 小乌龟
- Linux 命令整理 —— 基本操作
- 你不需要jQuery(五)
- php简单文件上传类
- FPGA的SPI从机模块实现
- 再说Java EE
- 【C++】处理CSDN博文源码
- knockout + easyui = koeasyui
- windows+Pycharm+Anaconda下安装opencv
- cuda、cuDNN的相关内容
- sql循环查询树形结构
- webpack配置常用loader加载器
- Oracle学习笔记:使用replace、regexp_replace实现字符替换、姓名脱敏
热门文章
- 小白也能看懂的mySQL进阶【单表查询】
- C++编程指南(6-7)
- 性能测试学习之路 (二)jmeter详解(jmeter执行顺序 &;&; 作用域 &;&; 断言 &;&; 事务 &;&;集合点 )
- pytorch Dataset Dataloader用法(一个示例)
- 微信小程序中使用text-indent实现首行缩进
- JOISC2020 自闭记
- 深入理解Java虚拟机(三)——垃圾回收策略
- Fabric v2.0中的隐私数据
- (干货)构建镜像之docker commit
- TimSort源码详解