spring-ApplicationContext的事件传播(转)
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的事件传播(转)的更多相关文章
- Spring ApplicationContext的事件机制
ApplicationContext的事件机制是观察者设计模式的实现,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现 ApplicationCon ...
- spring发布和接收定制的事件(spring事件传播)
spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报 分类: 开源技术(如Struts/spring/Hibernat ...
- spring发布和接收定制的事件(spring事件传播)[转]
有事件,即有事件监听器. 有人问你spring监听器有哪些你看了下文即也知道了. 事件传播 ApplicationContext基于Observer模式(java.util包中有对应实现),提供了 ...
- Spring中ApplicationContext对事件的支持
Spring中ApplicationContext对事件的支持 ApplicationContext具有发布事件的能力.这是因为该接口继承了ApplicationEventPublisher接口. ...
- Spring ApplicationContext(八)事件监听机制
Spring ApplicationContext(八)事件监听机制 本节则重点关注的是 Spring 的事件监听机制,主要是第 8 步:多播器注册:第 10 步:事件注册. public void ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- Spring ApplicationContext 简解
ApplicationContext是对BeanFactory的扩展,实现BeanFactory的所有功能,并添加了事件传播,国际化,资源文件处理等. configure locations:(C ...
- Spring ApplicationContext 简介
ApplicationContext是对BeanFactory的扩展,实现BeanFactory的所有功能,并添加了事件传播,国际化,资源文件处理等. configure locations:(C ...
- JavaEE开发之Spring中的事件发送与监听以及使用@Profile进行环境切换
本篇博客我们就来聊一下Spring框架中的观察者模式的应用,即事件的发送与监听机制.之前我们已经剖析过观察者模式的具体实现,以及使用Swift3.0自定义过通知机制.所以本篇博客对于事件发送与监听的底 ...
- 三种方式实现观察者模式 及 Spring中的事件编程模型
观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...
随机推荐
- Mysql学习(一)之简单介绍
数据库简介 数据库分类 关系型数据库:MySQL.Oracle.SQLServer.Access.db2.fox pro 文件型数据库:sqlite.mongodb 空间型数据库: 数据库分为两端 数 ...
- 09 Scrapy框架以及基本使用
一.什么是scrapy? 是为了爬取网站数据,提取结构性数据而编写的应用框架.之所以叫做框架是因为集成了各种实用功能(高性能异步下载,队列,分布式,解析,持久化等等)的项目模板.对于框架的学习,重点是 ...
- Spring的AOP,Struts2的拦截器(Interceptor),以及springMVC的(interceptor)
参考外链:http://www.ibm.com/developerworks/cn/java/j-lo-springaopfilter/ 1.首先,spring的AOP作用范围很广,可以使用Aspec ...
- selenium入门学习
在写爬虫的学习过程中,经常会有一些动态加载,有些是可以动过接口直接获取到,但是实在没办法,所以学习下selenium. 首先百度一下: Selenium [1] 是一个用于Web应用程序测试的工具. ...
- h5图片展示和ajax上传
<img src="" id="img"/> <script src="http://static.lamian.tv//pc/pu ...
- U-boot新手入门,烧写进mini2440
拿到一块开发板,首先就要找到它的资料,当然了,开发板的厂商或者代理商会提供资料,资料里会有你需要的. 比如我的这块mini2440,在友善之臂代理商提供的资料里面,就有我们这篇所需要的 把这个文件夹下 ...
- composer问题集锦
问题一:composer遇到Your configuration does not allow connection to 解决方案: 设置一个本地或全局的composer配置: composer c ...
- windows server没有这台电脑图标
rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0
- 第二章 Vue快速入门-- 24 过滤器-Vue中全局过滤器的基本使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- eclipse编码格式(中文乱码)
https://jingyan.baidu.com/article/2009576193ee38cb0721b416.html 修改工作空间默认编码 1 进入Eclipse,导入一个项目工程,如果项目 ...