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 ...
随机推荐
- yii2场景的应用(scenarios)
例如: 现在在 post表里面有 title image content 三个的字段,当我创建一个 post 的时候,我想三个字段全部是必填项,但是你修改的时候,title content 两个字段是 ...
- Java爬取校内论坛新帖
Java爬取校内论坛新帖 为了保持消息灵通,博主没事会上上校内论坛看看新帖,作为爬虫爱好者,博主萌生了写个爬虫自动下载的想法. 嗯,这次就选Java. 第三方库准备 Jsoup Jsoup是一款比较好 ...
- nginx -s reload时出现open() "/run/nginx.pid" failed (2: No such file or directory)错误
解决办法: 找到你的nginx.conf的文件夹目录,比如我的为/etc/nginx/nginx.conf,然后运行这个 nginx -c /etc/nginx/nginx.conf命令, 再运行 ...
- 使用idea+springboot+Mybatis搭建web项目
使用idea+springboot+Mybatis搭建web项目 springboot的优势之一就是快速搭建项目,省去了自己导入jar包和配置xml的时间,使用非常方便. 1.创建项目project, ...
- ARMv8学习 —— SP_EL0和SP_ELx
在AArch64状态下,SP对应的物理寄存器有如下四个(某一时刻只能对应下面其中一个): SP_EL0和SP_EL1 SP_EL2 SP_EL3 如何使用呢? 1.如果程序运行在EL0,那么使用的是S ...
- 如何确定一台linux主机是Linux (i386/i686)还是Linux (x86_64)
在下软件包的时候,往往会遇到一个选择: 假设自己的主机是Linux,那么Linux (i386/i686)和Linux (x86_64)究竟应该选哪一个呢? 针对当今的硬件而言,如果你主机的CPU是6 ...
- Win10系统的SurfacePro4如何重装系统-1 SurfacePro专用的PE
下载SurfacePro专用的PE(普通的PE可能不支持触摸屏操作,甚至没法启动Surface,所以务必要重新制作PE),下面提供百度云下载地址,下载之后,双击EXE,会进行检测 链接:https:/ ...
- js传递数组
注意:数组不能直接作为参数传给后台,需要先用JSON处理成数组字符串,传到后台后再转回数组. var arr_str = JSON.stringify(array)
- 在netty3.x中存在两种线程:boss线程和worker线程。
在netty 3.x 中存在两种线程:boss线程和worker线程.
- 单片机成长之路(51基础篇) - 013 MCS-51单片机控制详解–T2MOD
T2CON:定时器控制寄存器 寄存器地址0C8H,位寻址0C8H-0CFH. 位地址 CF CE CD CC CB CA C9 C8 位符号 TF2 EXF2 RCLK TCLK EXEN2 TR2 ...