原理: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实现的更多相关文章

  1. Spring中ApplicationEvent和ApplicationListener封装

    1.测试程序EventTest.java,发布一个事件只需要调用FrameEventHolder.publishEvent()方法即可. package com.junge.spring.event; ...

  2. Spring 的 ApplicationEvent and ApplicationListener

    什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环境,可是理解成容器会更好些. ApplicationContext则是应用的容器. Sprin ...

  3. spring之ApplicationEvent 事件驱动

    什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环境,但是理解成容器会更好些. ApplicationContext则是应用的容器. Sprin ...

  4. Spring事件,ApplicationEvent在业务中的应用

    前言 关于事件驱动模型,百度百科在有明确的解释.在JDK的Util包里抽象了事件驱动,有兴趣的朋友可以自行去看下相关类的定义.Spring事件模型ApplicationEvent是基于JDK里的事件模 ...

  5. 利用Spring的ApplicationEvent执行自定义方法

    在Spring中已经定义了五个标准事件,分别介绍如下: 1)ContextRefreshedEvent:当ApplicationContext初始化或者刷新时触发该事件. 2)ContextClose ...

  6. 译:Spring框架参考文档之IoC容器(未完成)

    6. IoC容器 6.1 Spring IoC容器和bean介绍 这一章节介绍了Spring框架的控制反转(IoC)实现的原理.IoC也被称作依赖注入(DI).It is a process wher ...

  7. Spring IOC 之ApplicationContext的其他功能

    正如上面章节所介绍的那样, org.springframework.beans.factory 包提供了管理和操作beans的 基本功能. org.springframework.context包增加 ...

  8. Re:从零开始的Spring Session(一)

    Session和Cookie这两个概念,在学习java web开发之初,大多数人就已经接触过了.最近在研究跨域单点登录的实现时,发现对于Session和Cookie的了解,并不是很深入,所以打算写两篇 ...

  9. 使用spring的监听器来完成系统超级管理员的注册

    1.注入 2“util类 package com.liveyc.mgrsite.util; import org.springframework.beans.factory.annotation.Au ...

随机推荐

  1. yii2 创建模块modules

    方案一:如果模块儿较少,不用专门给模块儿目录定义别名,酱紫做就ok啦. 1.在项目根目录下面创建一个 modules 目录. 2.进入 gii : http://localhost/basic/web ...

  2. Mac下hadoop运行word count的坑

    Mac下hadoop运行word count的坑 Word count体现了Map Reduce的经典思想,是分布式计算中中的hello world.然而博主很幸运地遇到了Mac下特有的问题Mkdir ...

  3. Codeforces Round #443 (Div. 1) D. Magic Breeding 位运算

    D. Magic Breeding link http://codeforces.com/contest/878/problem/D description Nikita and Sasha play ...

  4. db2删除表中数据

    DB2数据库系统建表后,难免会有将表中数据清空的需求,本文将为您介绍DB2数据库中四种数据删除的方法,供您参考,您可以根据实际情况来进行选择,希望能对有所帮助. 1. 使用 DELETE 语句,即: ...

  5. bootstrap常见的面试题

    1.  如果让一个元素在pc端显示而在手机端隐藏,下列选项正确的是(b). A. visible-xs-8  hidden-md B. visible-md-8 hidden-xs C. visibl ...

  6. pygame 笔记-8 背景音乐&子弹音效

    游戏哪能没有音效?这节我们研究下如何加背景音乐,其实也很简单: # 加载背景音乐 pygame.mixer.music.load(music_base_path + "music.mp3&q ...

  7. C# CountdownEvent实现

    关于CountdownEvent网上的介绍比较少,因为它是实现和使用都很简单,先看看网上的一些评论吧: CountDownEvent调用成员函数Wait()将阻塞,直至成员函数Signal() 被调用 ...

  8. gitbook安装与使用,并使用docker部署

    本文简单介绍如何安装并使用gitbook,最后如何使用docker构建书籍镜像. 1. 前置条件 需要Nodejs环境,安装npm,国内用户再安装cnpm npm install -g cnpm -- ...

  9. Django项目中自定义manage命令

    挺不错的一篇文章:https://www.cnblogs.com/ajianbeyourself/p/3643304.html

  10. Windows下安装模块mysqlclient报错处理

    Windows环境下使用pip命令安装Python模块mysqlclientpip install mysqlclient安装过程报错  [error] Microsoft Visual C++ 14 ...