Spring的ApplicationEvent的使用

Spring 3.0中提供了很多类似*Aware的类,其中ApplicationContextAware接口可以实现我们在初始化bean的时候给bean注入ApplicationConxt(Spring上下文对象)对象。ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。因此,我们只需要构造好我们自己的ApplicationEvent和ApplicationListener,就可以在系统中实现相应的监听器。

下面以增加学生的示例来演示如何构造Spring的监听器,StudentAddEvent是监听的事件对象,StudentAddListener是事件的监听器(负责处理接收到的监听事件),StudentAddBean负责触发StudentAddEvent事件。具体步骤如下:

1.  定义StudentAddEvent监听事件

新建StudentAddEvent类,实现抽象类

org.springframework.context.ApplicationEvent

StudentAddEvent类中需要实现自己的构造函数,具体代码如下:

[java] view plaincopy
  1. package com.trs.spring.event;
  2. import org.springframework.context.ApplicationEvent;
  3. /**
  4. * 增加学生的监听事件
  5. */
  6. public class StudentAddEvent extends ApplicationEvent {
  7. /**
  8. *
  9. */
  10. private static final long serialVersionUID = 20L;
  11. /**
  12. * 学生姓名
  13. */
  14. private String m_sStudentName;
  15. /**
  16. * @param source
  17. */
  18. public StudentAddEvent(Object source, String _sStudentName) {
  19. super(source);
  20. this.m_sStudentName = _sStudentName;
  21. }
  22. /**
  23. * 获取学生姓名
  24. *
  25. * @return
  26. */
  27. public String getStudentName() {
  28. return m_sStudentName;
  29. }
  30. }

2.  定义StudentAddListener监听器

新建StudentAddListener类,实现接口

org.springframework.context.ApplicationListener

中的onApplicationEvent方法,在该方法中只处理StudentAddEvent类型的ApplicationEvent事件,代码如下:

[java] view plaincopy
  1. package com.trs.spring.event;
  2. import org.springframework.context.ApplicationEvent;
  3. import org.springframework.context.ApplicationListener;
  4. public class StudentAddListener implements ApplicationListener {
  5. /*
  6. * (non-Javadoc)
  7. *
  8. * @see
  9. * org.springframework.context.ApplicationListener#onApplicationEvent(org
  10. * .springframework.context.ApplicationEvent)
  11. */
  12. public void onApplicationEvent(ApplicationEvent _event) {
  13. // 1.判断是否是增加学生对象的事件
  14. if (!(_event instanceof StudentAddEvent)) {
  15. return;
  16. }
  17. // 2.是增加学生事件的对象,进行逻辑处理,比如记日志、积分等
  18. StudentAddEvent studentAddEvent = (StudentAddEvent) _event;
  19. System.out.println("增加了学生:::" + studentAddEvent.getStudentName());
  20. }
  21. }

3.  定义StudentAddBean触发StudentAddEvent事件

新建StudentAddBean类,实现接口

org.springframework.context.ApplicationContextAware

中的setApplicationContext方法,在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件,具体代码如下:

[java] view plaincopy
  1. package com.trs.spring.event;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.ApplicationContextAware;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class StudentAddBean implements ApplicationContextAware {
  7. /**
  8. * 定义Spring上下文对象
  9. */
  10. private ApplicationContext m_applicationContext = null;
  11. /*
  12. * (non-Javadoc)
  13. *
  14. * @see
  15. * org.springframework.context.ApplicationContextAware#setApplicationContext
  16. * (org.springframework.context.ApplicationContext)
  17. */
  18. public void setApplicationContext(ApplicationContext _applicationContext)
  19. throws BeansException {
  20. this.m_applicationContext = _applicationContext;
  21. }
  22. /**
  23. * 增加一个学生
  24. *
  25. * @param _sStudentName
  26. */
  27. public void addStudent(String _sStudentName) {
  28. // 1.构造一个增加学生的事件
  29. StudentAddEvent aStudentEvent = new StudentAddEvent(
  30. m_applicationContext, _sStudentName);
  31. // 2.触发增加学生事件
  32. m_applicationContext.publishEvent(aStudentEvent);
  33. }
  34. /**
  35. * @param args
  36. */
  37. public static void main(String[] args) {
  38. String[] xmlConfig = new String[] { "applicationContext.xml" };
  39. // 使用ApplicationContext来初始化系统
  40. ApplicationContext context = new ClassPathXmlApplicationContext(
  41. xmlConfig);
  42. StudentAddBean studentBean = (StudentAddBean) context
  43. .getBean("StudentAddBean");
  44. studentBean.addStudent("我是第一个学生");
  45. studentBean.addStudent("第二个学生已经添加");
  46. }
  47. }

4.  applicationContext.xml配置文件

<bean id="StudentAddBean" class="com.trs.spring.event.StudentAddBean"></bean>

<bean id="StudentAddListener" class="com.trs.spring.event.StudentAddListener"></bean>

 

5.  说明

ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象,每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。

6.  执行StudentAddBean的main函数,结果如下:

增加了学生:::我是第一个学生

增加了学生:::第二个学生已经添加

7.  测试工程下载地址:

下载地址:http://download.csdn.net/detail/wgw335363240/4022181

来源: <http://blog.csdn.net/wgw335363240/article/details/7202320#>

Spring的ApplicationEvent的使用相关推荐

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

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

  2. 2.15 Spring Framework 5.x 之ApplicationContext附加功能

    1.15 附加功能ApplicationContext 正如章节介绍中所讨论的,该org.springframework.beans.factory 包提供了管理和操作bean的基本功能,包括以编程方 ...

  3. spring framwork-ioc容器

    参考文档的这一部分涵盖了Spring框架中不可或缺的所有技术. 其中最重要的是Spring框架的控制反转(IoC)容器.在对Spring框架的IoC容器进行了全面的处理之后,还对Spring的面向方面 ...

  4. Spring源码之The IoC container官方文档翻译

    官方文档:https://docs.spring.io/spring/docs/4.3.21.RELEASE/spring-framework-reference/htmlsingle/#beans ...

  5. 【翻译 Spring 5.0.4.RELEASE】1.The IoC container

    其中最重要的是Spring框架的控制反转(IoC)容器. Spring框架的IoC容器的全面处理紧随其后,全面涵盖了Spring的面向方面编程(AOP)技术. Spring框架拥有自己的AOP框架,这 ...

  6. Spring核心技术IOC

    Spring核心技术之IOC 本节目的:IOC作为spring一切的核心,了解内部机制很有必要的.本节会对照官方文档目录加上自己理解为主,将IOC的细节,尽可能地挖掘出来,让大家精通IOC的高级应用, ...

  7. spring core之Ioc介绍

    1.ApplicationContext是BeanFactory的子接口. 2.BeanFactory提供配置框架和基本功能,ApplicationContext添加更多特定于企业的功能. 3.org ...

  8. 看完这篇,code review 谁敢喷你代码写的烂?怼回去!

    我将常用的软件设计模式,做了汇总,目录如下: (考虑到内容篇幅较大,为了便于大家阅读,将软件设计模式系列(共23个)拆分成四篇文章,每篇文章讲解六个设计模式,采用不同的颜色区分,便于快速消化记忆) 本 ...

  9. 一起学spring--spring事件机制--监听器

    欢迎进入<一起学spring>系列博文第三篇, spring容器的事件监听机制,同样有事件.事件源和监听者.而spring中的事件需要继承ApplicationEvent,监听者需要继承A ...

最新文章

  1. opencv3.4.1 vs2017 鼠标停在 Mat 上 vs卡死
  2. 《从零构建前后分离web项目》:开篇 - 纵观WEB历史演变
  3. linux一个网卡绑定多个端口,RedHat实现多网卡绑定
  4. Android -- Camera聚焦流程
  5. Spring学习(三)Spring AOP 简介
  6. C# 是否可以将 动态或匿名类型 转成 强类型 ?
  7. (实用篇)浅谈PHP拦截器之__set()与__get()的理解与使用方法
  8. java正立三角形_java for循环练习(9*9乘法表、正三角形、菱形)
  9. 百度头条正式火拼:尔要战,便战!
  10. MATLAB常见问题:小数保留有效数字位数相关问题/除法结果问题/数据显示格式设置
  11. c语言sinx的幂级数展开式,sinx的幂级数展开式问题?
  12. js url解码gbk_使用js解码url里的gbk汉字编码
  13. HDU 5857 Median(水~)
  14. 亚马逊自然语言NLP 商品评论智能分析 demo及开发过程 【1 总体API介绍】【持续更新中】
  15. 电脑it族宝典常用电脑的从怎样保护自己的眼睛
  16. Intel opencl sdk下载安装
  17. word图文混排复制到eWebEditor图片不显示
  18. VSCode的下载安装与配置教程(详细)
  19. H.264里的SEI——自定义信息(SEI)
  20. 图片介绍html,HTML图片介绍

热门文章

  1. mysql_ping()函数的作用以及返回值的类型正确的是,[单选] mysql_ping()函数的作用以及返回值的类型正确的是:()...
  2. 贪吃蛇程序 php,微信小程序-贪吃蛇教程实例
  3. sklearn 实例
  4. long类型20位示例_Java Long类reverseBytes()方法与示例
  5. php unset函数_PHP | 使用unset()函数从数组中删除元素
  6. oracle10数据库链接失败,Oracle10g出现Enterprise Manager 无法连接到数据库实例解决办法...
  7. 华为P40pro 手机云台_2020年目前拍照最好的手机推荐!华为P40 Pro!DXO全球榜首
  8. 华为鸿蒙系统正式登场,华为自研鸿蒙系统将于8月9日正式登场,还有全新的鸿鹄芯片...
  9. 全连接条件随机场_条件随机场CRF简介
  10. python提取英文单词 每行显示一个_使用python对文件中的单词进行提取