Spring事件监听Demo
Spring事件监听实现了观察者模式。本Demo在junit4测试环境中实现
主要有三个类事件类、监听器类、事件发布类(入口)
事件类必须继承 ApplicationEvent,代码如下:
import org.junit.runner.RunWith; import org.springframework.context.ApplicationEvent; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** Created by *** on 2016/3/15.*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml") public class StudentAddEventTest extends ApplicationEvent {private String sname;public StudentAddEventTest(Object source, String sname) {super(source);this.sname = sname;}public String getSname() {return sname;} }
监听器Listener类需实现 ApplicationListener 接口 ApplicationListener可以传入一个泛型的事件类型变量,也可以不传。不传的话,需要在监听到事件发生时(onApplicationEvent)自己判断该事件是不是自己要监听的事件。
import org.junit.runner.RunWith; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** Created by *** on 2016/3/15.*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml") @Component public class StudentAddListenerTest implements ApplicationListener<StudentAddEventTest> {@Overridepublic void onApplicationEvent(StudentAddEventTest studentAddEventTest) {String sname = studentAddEventTest.getSname();System.out.println("增加的学生的名字为:::"+sname);} }
事件发布类实现了 ApplicationContextAware ,实现这个主要是为了拿到 ApplicationContext实例,进而调用他的 publishEvent方法。感觉不实现ApplicationContextAware应该也可以。。。
:
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** Created by *** on 2016/3/15.*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml") @Component public class StudentAddBeanTest implements ApplicationContextAware {private static ApplicationContext applicationContext;@Override@Autowiredpublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}public void addStudent(String sname){StudentAddEventTest addEvent = new StudentAddEventTest(this, sname);applicationContext.publishEvent(addEvent);}@Testpublic void addTest(){StudentAddBeanTest studentBean = new StudentAddBeanTest();studentBean.addStudent("张三");studentBean.addStudent("李四");}}
需要注意的是 StudentAddListenerTest ——Listener类,StudentAddBeanTest ——事件发布类需在spring容器中注册,Demo中在 applicationContext-indexConfig-test.xml配置了
扫描<context:component-scan> </context:component-scan>路径
然后在类上加了注解@Component,运行测试方法addTest()经过测试,能顺利实现既定目标。
转载于:https://www.cnblogs.com/winkey4986/p/5279502.html
Spring事件监听Demo相关推荐
- spring 事件监听
用一个简单的例子来实现spring事件监听的功能 这个例子主要功能是,记录那些用户是第一次登入系统,如果用户是第一次登入系统,则调用spring的事件监听,记录这些用户. 主要用到的spring的类和 ...
- Spring5源码 - 13 Spring事件监听机制_@EventListener源码解析
文章目录 Pre 概览 开天辟地的时候初始化的处理器 @EventListener EventListenerMethodProcessor afterSingletonsInstantiated 小 ...
- Spring5源码 - 12 Spring事件监听机制_异步事件监听应用及源码解析
文章目录 Pre 实现原理 应用 配置类 Event事件 事件监听 EventListener 发布事件 publishEvent 源码解析 (反推) Spring默认的事件广播器 SimpleApp ...
- Spring5源码 - 11 Spring事件监听机制_源码篇
文章目录 pre 事件监听机制的实现原理[观察者模式] 事件 ApplicationEvent 事件监听者 ApplicationEvent 事件发布者 ApplicationEventMultica ...
- Spring事件监听原理
1 简述Spring的生命周期 不论是Spring的监听机制原理还是Spring AOP的原理,都是依托于Spring的生命周期,所以要了解Spring的监听机制原理就需要先了解Spring的生命周期 ...
- Spring事件监听流程分析【源码浅析】
一.简介 Spring早期是通过实现ApplicationListener接口来定义监听事件,在spring4.2的时候开始我们可以通过@EventListener注解来定义监听事件,Applicat ...
- Spring5源码 - 10 Spring事件监听机制_应用篇
文章目录 Spring事件概览 事件 自定义事件 事件监听器 基于接口 基于注解 事件广播器 Spring事件概览 Spring事件体系包括三个组件:事件,事件监听器,事件广播器 事件 Spring的 ...
- 中规中矩的Spring事件监听
首先创建需要监听的事件类 import org.springframework.context.ApplicationEvent; public class GoodsEvent extends Ap ...
- spring 扫描所有_自定义Spring事件监听机制
开头提醒一下大家: 尽管我简化了Spring源码搞了个精简版的Spring事件机制,但是没接触过Spring源码的朋友阅读起来还是有很大难度,请复制代码到本地,边Debug边看 既然要简化代码,所以不 ...
- Spring之事件监听
spring的监听事件模型应该是观察者模式.本人项目里的应用在process方法按日期同步某个库的数据,在方法最后publish一个event事件,这个事件用于后续同步过来的数据处理.事件处理采用了s ...
最新文章
- 火绒规则 禁止所有软件的安装_软件: Firefox 将禁止扩展静默安装
- 如何在html中写javascript代码,如何在本地html代码中使用javascript脚本
- laravel的carbon日期函数创建指定的now()日期以及遇到的一些坑
- 简约之美Jodd-http--深入源码理解http协议
- 数字化测图是利用计算机自动绘制地形图,运用VirtuoZo软件实现航空摄影测量数字化测图...
- 【代码规范】常见排版规范
- Java ObjectOutputStream flush()方法与示例
- 推荐系统——Wide Deep
- java代码ftp重命名未生效_java实现FTP远程文件移动(重命名、复制、拷贝) | 学步园...
- 对一个可进行带括号 加减乘除运算类的分析
- [设计模式-结构型]外观模式(Facade)
- selenuim webDriver API 16种定位方式
- axure rp 9授权码
- 移动硬盘需要格式化才能打开如何解决?
- jmeter ramp-up-period设置以及同步定时器使用
- Mac键盘突然失灵怎么办?别急,教你打开辅助键盘
- a = a + 1,a+=1, a++, ++a 区别在哪
- SQL优化13连问,收藏好!
- 我用Python爬取了4000+股票数据,并用plotly绘制了树状热力图(treemap)
- Unity中游戏存档方式
热门文章
- EmEditor 常用功能
- 一个c3p0的数据库连接池的多线程测试
- WinDbg使用摘要
- base64与base64url编码
- python 列表、字典
- C++ 变量在内存中的分布
- 优达学城 python 人工智能入门_深度学习TensorFlow入门资源汇总-如何从零开发人工智能...
- readhat7mysql_Redhat7.3安装MySQL8.0.22的详细教程(二进制安装)
- 3.5用户访问使用HTTPS
- 多臂老虎机导论(二)Stochastic Bandits