Spring中ApplicationContext对事件传递
来源:互联网 发布:怎么禁止mac休眠 编辑:程序博客网 时间:2024/05/19 11:50
在这里包含三个角色:被发布的事件,事件的监听者和事件发布者。
事件发布者在发布事件的时候通知事件的监听者。
下面我们围绕这三个角色进行分析:
首先是被发布的事件:在Spring中,这个角色继承了ApplicationEvent类。
再看监听者,监听者实现了ApplicationListener接口。
而事件的发布者则实现了ApplicationContextAware接口,并调用publishEvent方法发布事件。在这里,事件将作为参数传递到这个方法中。
ApplicationContext具有发布事件的能力。这是因为该接口继承了ApplicationEventPublishe
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或者ApplicationEventPublishe
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); .publishEvent(event);} applicationContext }
或者如下:
public class UserBiz2 implements ApplicationEventPublisherAware {private ApplicationEventPublisher applicationEventPublisher ;public void setApplicationEventPublisher (ApplicationEventPublisher applicationEventPublisher ){this.applicationEventPublisher = applicationEventPublisher ;}public void service(String thing){UserEvent event = new UserEvent(this,thing); event.setEventContent("I shoud "+thing); .publishEvent(event);}} applicationEventPublishe r
至此便完成了事件的发布,当ApplicationContext接收到事件后,事件的广播是Spring内部给我们做的,不需要了解具
体的细节。其实在Spring读取配置文件之后,利用反射,将所有实现ApplicationListener的Bean找出来,注册为容器
的事件监听器。当接收到事件的时候,Spring会逐个调用事件监听器。剩下要做的就是在配置文件中配置监听器。
class="footprint.spring.ioc.event.UserListener"/>
Spring容器自身会发布一些事件,包括ContextClosedEvent、ContextRefreshedEvent、ContextStartedEvent、
ContextStoppedEvent。
- Spring中ApplicationContext对事件传递
- Spring中ApplicationContext对事件传递
- Spring中ApplicationContext对事件的支持 Spring 中事件的传递
- 详解Spring事件驱动模型-Spring中ApplicationContext对事件的支持
- Spring中ApplicationContext的事件机制
- Spring中ApplicationContext的事件机制 .
- Spring中ApplicationContext的事件机制
- Spring中ApplicationContext的事件机制
- Spring中ApplicationContext的事件机制
- Spring中ApplicationContext对Beanfactory扩展
- Spring--> ApplicationContext事件传播
- Spring ApplicationContext事件机制
- Spring-ApplicationContext事件机制
- Spring ApplicationContext事件机制
- Spring中ApplicationContext的事件机制(三 自定义事件)
- 【好文推荐】Spring中ApplicationContext的事件机制
- Spring---ApplicationContext的事件机制
- spring中取出ApplicationContext
- android.content.res.Resources$NotFoundException:
- 最小生成树(题解) 两种解法
- Happy Number
- Test,test
- 网络请求 同步get 同步post 异步get 异步post
- Spring中ApplicationContext对事件传递
- Nginx基本命令
- 110Balanced Binary Tree
- I NEED A OFFER! 动态规划 01 背包
- Jdbc和Odbc基本操作
- 第三天
- DDuilib入门-XML布局【二】
- Android:AsyncTask 随记
- 读书笔记 -- 008_数据挖掘_聚类_基于层次的方法