ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供的,通过ApplicationContext的publishEvent()方法发布到ApplicationListener; 在这里包含三个角色:被发布的事件,事件的监听者和事件发布者。

事件发布者在发布事件的时候通知事件的监听者。

下面我们围绕这三个角色进行分析: 首先是被发布的事件:在Spring中,这个角色继承了ApplicationEvent类。 再看监听者,监听者实现了ApplicationListener接口。 而事件的发布者则实现了ApplicationContextAware接口,并调用publishEvent方法发布事件。

在这里,事件将作为参数传递到这个方法中。

ApplicationContext具有发布事件的能力。这是因为该接口继承了ApplicationEventPublisher接口。Spring中与事件有关的接口和类主要包括ApplicationEvent、ApplicationListener。定义一个事件的类需要继承ApplicationEvent或者ApplicationContextEvent抽象类,

该抽象类中只有一个构造函数,并且带有一个Object类型的参数作为事件源,并且该事件源不能为null,因此我们需要在自己的构造函数中执行super(Object)。

 public class UserEvent extends ApplicationEvent

 {

   private String eventContent;

   public String getEventContent(){

      return eventContent;

   }

   public void setEventContent(String eventContent){

      this.eventContent = eventContent;

   }

   public UserEvent(Object source,String eventContent){

      super(source);

      this.eventContent = eventContent;

   }

 }

针对一种事件,可能需要特定的监听器,因此,监听器需要实现ApplicationListener接口。当监听器接收到一个事件的时候,就会执行它的 onApplicationEvent()方法。由于SpringIoC中的事件模型是一种简单的、粗粒度的监听模型,当有一个事件到达时,所有的监听器都会接收到,

并且作出响应,如果希望只针对某些类型进行监听,需要在代码中进行控制。

 public class UserListener implements ApplicationListener

 {

   public void onApplicationEvent(ApplicationEvent event){

      if(event instanceof UserEvent){ //只对UserEvent类型进行处理

         UserEvent ue = (UserEvent)event;

         String result = ue.getEventContent();

         System.out.println("Event Content:"+result);

      }

   }

 }

对于发布事件,我们可以实现ApplicationContextAware或者ApplicationEventPublisherAware接口

 public class UserBiz implements ApplicationContextAware

 {

 private ApplicationContext applicationContext;

 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException

 {

    this.applicationContext = applicationContext; 

 }

 public void service(String thing)

 {

    UserEvent event = new UserEvent(this,thing);

    event.setEventContent("I shoud "+thing);

     applicationContext.publishEvent(event);

 }  

 }

至此便完成了事件的发布,当ApplicationContext接收到事件后,事件的广播是Spring内部给我们做的,不需要了解具体的细节。其实在Spring读取配置文件之后,利用反射,将所有实现ApplicationListener的Bean找出来,注册为容器的事件监听器。当接收到事件的时候,

Spring会逐个调用事件监听器。剩下要做的就是在配置文件中配置监听器

spring-ApplicationContext的事件传播(转)的更多相关文章

  1. Spring ApplicationContext的事件机制

    ApplicationContext的事件机制是观察者设计模式的实现,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现 ApplicationCon ...

  2. spring发布和接收定制的事件(spring事件传播)

    spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报  分类: 开源技术(如Struts/spring/Hibernat ...

  3. spring发布和接收定制的事件(spring事件传播)[转]

    有事件,即有事件监听器. 有人问你spring监听器有哪些你看了下文即也知道了.   事件传播 ApplicationContext基于Observer模式(java.util包中有对应实现),提供了 ...

  4. Spring中ApplicationContext对事件的支持

    Spring中ApplicationContext对事件的支持   ApplicationContext具有发布事件的能力.这是因为该接口继承了ApplicationEventPublisher接口. ...

  5. Spring ApplicationContext(八)事件监听机制

    Spring ApplicationContext(八)事件监听机制 本节则重点关注的是 Spring 的事件监听机制,主要是第 8 步:多播器注册:第 10 步:事件注册. public void ...

  6. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  7. Spring ApplicationContext 简解

    ApplicationContext是对BeanFactory的扩展,实现BeanFactory的所有功能,并添加了事件传播,国际化,资源文件处理等.   configure locations:(C ...

  8. Spring ApplicationContext 简介

    ApplicationContext是对BeanFactory的扩展,实现BeanFactory的所有功能,并添加了事件传播,国际化,资源文件处理等.   configure locations:(C ...

  9. JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换

    本篇博客我们就来聊一下Spring框架中的观察者模式的应用,即事件的发送与监听机制.之前我们已经剖析过观察者模式的具体实现,以及使用Swift3.0自定义过通知机制.所以本篇博客对于事件发送与监听的底 ...

  10. 三种方式实现观察者模式 及 Spring中的事件编程模型

    观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...

随机推荐

  1. ES6 新增的数组的方法

    给定一个数组 let list = [ // wu: 武力 zhi:智力 { id: 1, name: '张飞', wu: 97, zhi: 10 }, { id: 2, name: '诸葛亮', w ...

  2. WPF:元素绑定

    到目前为止都在讨论如何链接两个元素的绑定.但在数据驱动的应用程序中,更常见的情况是创建从不可见的对象中提取数据绑定表达式.唯一的要求是希望显示的信息必须存储在公有的属性中.WPF数据绑定基础结构不能获 ...

  3. docker删除虚悬镜像(临时镜像文件)

    在我们构建镜像的过程中,常常需要使用build命令根据Dockerfile进行构建镜像,并且会build很多次,镜像名字也是相同的,那么就会出来下面这种情况

  4. python、mysql三-3:完整性约束

    一 介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...

  5. Mac下的LDAP客户端 ApacheDirectoryStudio

    mac下的ldap browser,最开始下载的最新版本的 地址 http://directory.apache.org/studio/downloads.html 使用的时候经常卡死,尝试下载老版本 ...

  6. 时序数据库influxDB存储数据grafana展示数据

    一.influxDB简介 InfluxDB是一款用Go语言编写的开源分布式时序.事件和指标数据库,无需外部依赖.该数据库现在主要用于存储涉及大量的时间戳数据,如DevOps监控数据,APP metri ...

  7. 树形DP Choosing Capital for Treeland

    给你一棵有向树,需要选定一个点为capital,满足翻转边数最小 思路:先求出1为capital 的答案,然后向下更新孩子节点 dp[i]=dp[i-1]+judge(i); #include< ...

  8. Django学习系列12:把Python变量传入模板中渲染

    从视图的Python代码中把变量传入HTML模板. 模板中使用哪种句法引入Python对象,要使用的符号{{...}},它会以字符串的形式显示对象: <html> <head> ...

  9. AT&T 和 Intel

  10. 洛谷P3370 && 字符串哈希讲解

    字符串哈希 寻找长度为n的主串s中的的匹配串T(长度为m)出现的位置或者次数问题属于字符串匹配问题. 朴素(一般)的想法就是从一个字符串的头开始for循环查找,当查找的一个字符与匹配串首字符相同时,往 ...