Spring的ApplicationEvent实现
原理:ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。
直接上代码:
1.定义自己的监听事件
2.定义自己的监听器(负责处理自己的监听事件)
3.定义一个bean触发监听事件
4.测试
package com.test.eventListener; import org.springframework.context.ApplicationEvent; /**
* @author admin
* @date 2018/5/17 17:37
* 新建StudentAddEvent类,实现抽象类org.springframework.context.ApplicationEvent
* StudentAddEvent类中需要实现自己的构造函数
* 增加学生监听事件
*/
public class StudentAddEvent extends ApplicationEvent { private static final long serialVersionUID = 20L; /**
* 学生姓名
*/
private String name; /**
* @param source
*/
public StudentAddEvent(Object source, String name) {
super(source);
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
package com.test.eventListener; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* @author admin
* @date 2018/5/17 17:41
* 新建StudentAddListener类,实现接口org.springframework.context.ApplicationListener中的onApplicationEvent方法,
* 在该方法中只处理StudentAddEvent类型的ApplicationEvent事件
* 定义StudentAddListener监听器
*/
@Component
public class StudentAddListener implements ApplicationListener { public void onApplicationEvent(ApplicationEvent event) {
// 1.判断是否是增加学生对象的事件
if (!(event instanceof StudentAddEvent)) {
return;
} // 2.是增加学生事件的对象,进行逻辑处理,比如记日志、积分等
StudentAddEvent studentAddEvent = (StudentAddEvent) event;
System.out.println("增加了学生:" + studentAddEvent.getName());
}
}
package com.test.eventListener; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* @author admin
* @date 2018/5/17 17:45
* 定义StudentAddBean触发StudentAddEvent事件
* 新建StudentAddBean类,实现接口 org.springframework.context.ApplicationContextAware中的setApplicationContext方法,
* 在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件
*/
@Component
public class StudentAddBean implements ApplicationContextAware {
/**
* 定义Spring上下文对象
*/
private ApplicationContext applicationContext = null; /*
* (non-Javadoc)
*
* @see
* org.springframework.context.ApplicationContextAware#setApplicationContext
* (org.springframework.context.ApplicationContext)
*/
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext; } /**
* 增加一个学生
*
* @param studentName
*/
public void addStudent(String studentName) {
// 1.构造一个增加学生的事件
StudentAddEvent aStudentEvent = new StudentAddEvent(
applicationContext, studentName);
// 2.触发增加学生事件
applicationContext.publishEvent(aStudentEvent);
} }
package com.test.eventListener; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author admin
* @date 2018/5/17 17:55
* ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。
* 当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象,
* 每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。
*/
public class EventListenerTest {
public static void main(String[] args) {
String[] xmlConfig = new String[] { "spring/spring.xml" };
// 使用ApplicationContext来初始化系统
ApplicationContext context = new ClassPathXmlApplicationContext(xmlConfig);
StudentAddBean studentBean = (StudentAddBean) context.getBean("studentAddBean");
studentBean.addStudent("张三");
studentBean.addStudent("李四");
}
}
Spring的ApplicationEvent实现的更多相关文章
- Spring中ApplicationEvent和ApplicationListener封装
1.测试程序EventTest.java,发布一个事件只需要调用FrameEventHolder.publishEvent()方法即可. package com.junge.spring.event; ...
- Spring 的 ApplicationEvent and ApplicationListener
什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环境,可是理解成容器会更好些. ApplicationContext则是应用的容器. Sprin ...
- spring之ApplicationEvent 事件驱动
什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些. ApplicationContext则是应用的容器. Sprin ...
- Spring事件,ApplicationEvent在业务中的应用
前言 关于事件驱动模型,百度百科在有明确的解释.在JDK的Util包里抽象了事件驱动,有兴趣的朋友可以自行去看下相关类的定义.Spring事件模型ApplicationEvent是基于JDK里的事件模 ...
- 利用Spring的ApplicationEvent执行自定义方法
在Spring中已经定义了五个标准事件,分别介绍如下: 1)ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件. 2)ContextClose ...
- 译:Spring框架参考文档之IoC容器(未完成)
6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...
- Spring IOC 之ApplicationContext的其他功能
正如上面章节所介绍的那样, org.springframework.beans.factory 包提供了管理和操作beans的 基本功能. org.springframework.context包增加 ...
- Re:从零开始的Spring Session(一)
Session和Cookie这两个概念,在学习java web开发之初,大多数人就已经接触过了.最近在研究跨域单点登录的实现时,发现对于Session和Cookie的了解,并不是很深入,所以打算写两篇 ...
- 使用spring的监听器来完成系统超级管理员的注册
1.注入 2“util类 package com.liveyc.mgrsite.util; import org.springframework.beans.factory.annotation.Au ...
随机推荐
- 单元测试-unittest
一.简介 unittest单元测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果. 二.属性介绍 1.unittest模块的各个属性 unittest.Tes ...
- spring boot 中添加mongodb支持
1.添加maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- Selenium + WebDriver 各浏览器驱动下载地址
Chrome 点击下载chrome的webdriver: http://chromedriver.storage.googleapis.com/index.html 不同的Chrome的版本对应的ch ...
- 使用wrk进行压力测试
最近需要对新的服务进行压力测试.比较了ab和jemeter以及wrk.最终选择wrk来作为压力测试工具,可以把cpu压到100%. 官方源码: https://github.com/wg/wrk 安装 ...
- 【纵谭 Python】系列直播(持续更新)
老周最近录了一些跟 Python 有关的直播,可以在“一直播”中搜索 ID 号 139251129 关注,也可以在微博中查看,反正都一样,同步的. 第一集:简单胡扯一下相关环境搭建.安装 Python ...
- Hibernate 离线对象构建通用查询
1.业务场景 当下主系统衍生子业务系统已经成为常态,像京东的物流和金融,阿里的支付宝和淘宝. 子业务系统需要对主系统的资源进行访问,这里的资源我具体化为数据库数据,但日常业务中可能不只是数据. 抽象服 ...
- 机器人关节空间轨迹规划--S型速度规划
关节空间 VS 操作空间 关节空间与操作空间轨迹规划流程图如下(上标$i$和$f$分别代表起始位置initial和目标位置final): 在关节空间内进行轨迹规划有如下优点: 在线运算量更小,即无需进 ...
- cmd命令行的ping用法
1.打开cmd 2.ping 域名 (如:ping baidu.com) 3.输出结果 C:\WINDOWS\system32>ping baidu.com 正在 Ping baidu.c ...
- 通过groovy表达式拓展oval——实现根据同一实体中的其他属性值对某个字段进行校验
在java的参数校验中,开源验证框架OVAL基本能够满足所有需求,如下面通过简单的添加注解,就可实现对参数的非空和长度校验. @NotNull(message="计息周期月数不能为空&quo ...
- vs2017 打开附带的localdb v13
查看自己本机装了哪些版本的localdb 打开数据库资源管理器 添加数据库连接 选择要连接的localdb版本,这里选择的是v13版本 添加数据库 新建一个test数据库 ...