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 ...
随机推荐
- MD5+Salt值
生成Salt值 package util; import java.util.Random; public class Salt { public String getSalt() { Random ...
- oracle函数返回结果集
一.用自定义类型实现 1.创建表对象类型. 在Oracle中想要返回表对象,必须自定义一个表类型,如下所示: create or replace type type_table is table of ...
- Mac Mysql 修改初始化密码
第一步: 点击系统偏好设置->最下边点MySQL,在弹出页面中,关闭服务 第二步:进入终端输入:cd /usr/local/mysql/bin/回车后 登录管理员权限 sudo su回车后输入以 ...
- Annotation 的第一个工程
一.什么是 Annotation? java.lang.annotation,接口 Annotation.对于Annotation,是Java5的新特性,JDK5引入了Metadata(元数据)很容易 ...
- Updating and Publishing a NuGet Package - Plus making NuGet packages smarter and avoiding source edits with WebActivator
I wrote a post a few days ago called "Creating a NuGet Package in 7 easy steps - Plus using NuG ...
- .Net转Java.04.踩到switch的坑
今天线上有个NullPointerException 的异常,我翻了一下代码,抛异常的竟然是switch语句 我有种不祥的预感,本地做了实验 结果是 Java的switch如果传入null值,会抛出 ...
- c++中transform()函数和find()函数的使用方法。
1.transform函数的使用 transform在指定的范围内应用于给定的操作,并将结果存储在指定的另一个范围内.transform函数包含在<algorithm>头文件中. 以下是s ...
- git stash命令详解
git stash命令用于将更改储藏在脏工作目录中. 使用语法 git stash list [<options>] git stash show [<stash>] git ...
- 集群安装Java环境
需要安装一个集群环境,发现全部要手动安装java.记录下安装Java环境的过程.虽然,依旧是挨个安装,但总算是有体系了. java 找到下载地址: https://www.oracle.com/tec ...
- 巧用watch命令执行循环操作,来解放我们的双手
有时候我们需要重复执行某个 命令,观察某个文件和某个结果的变化情况.可以写 脚本去实现这些需求,但是有更简单的方法,本文档要介绍的就是watch 命令. 1. 以固定时间反复执行某个命令 root@j ...