Spring中ApplicationContext对事件的支持
Spring中ApplicationContext对事件的支持
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()方法。由于Spring IoC中的事件模型是一种简单的、粗粒度的监听模型,当有一个事件到达时,所有的监听器都会接收到,并且作出响应,如果希望只针对某些类型进行监听,需要 在代码中进行控制。
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);
}
}
或者如下:
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);
applicationEventPublisher.publishEvent(event);
}
}
至此便完成了事件的发布,当ApplicationContext接收到事件后,事件的广播是Spring内部给我们做的,不需要了解具体的细节。其实在 Spring读取配置文件之后,利用反射,将所有实现ApplicationListener的Bean找出来,注册为容器的事件监听器。当接收到事件的 时候,Spring会逐个调用事件监听器。剩下要做的就是在配置文件中配置监听器。
<bean class="footprint.spring.ioc.event.UserListener"/>
Spring容器自身会发布一些事件,包括ContextClosedEvent、ContextRefreshedEvent、ContextStartedEvent、ContextStoppedEvent。
Spring中ApplicationContext对事件的支持的更多相关文章
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
- Spring中ApplicationContext加载机制和配置初始化
Spring中ApplicationContext加载机制. 加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet. ...
- Spring中ApplicationContext和beanfactory区别---解析一
BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...
- Spring中ApplicationContext和beanfactory区别
BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...
- Spring中对资源的读取支持
Resource简单介绍 注:所有操作基于配置好的Spring开发环境中. 在Spring中,最为核心的部分就是applicationContext.xml文件,而此配置文件中字符串的功能发挥到了极致 ...
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...
- Spring中ApplicationContext加载机制
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp33 加载器目前有两种选择:ContextLoaderListener和Co ...
- 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决
问题描述: 在配置spring的applicationContext.xml中的默认事务管理器的时候可能会出现这样的错误: Error occured processing XML 'org/spri ...
- 在Spring中使用异步事件实现同步事务
结合Scala+Spring,我们将采取一个很简单的场景:下订单,然后发送一封电子邮件. 编制一个服务: @Serviceclass OrderService @Autowired() (orderD ...
随机推荐
- 区间DP lightoj 1422
t个样例 n n个数字 从 1->n 穿衣服 脱了就不能再用 ,可以套 问最少几件衣服 #include<stdio.h> #include<string.h> # ...
- spoj687 后缀数组重复次数最多的连续重复子串
REPEATS - Repeats no tags A string s is called an (k,l)-repeat if s is obtained by concatenating k& ...
- java-正则表达式过滤字符串中的html标签
案例 import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <p> * Title: HTML相关的正 ...
- HTTP协议学习---(十)拓展-HTTPS协议
HTTPS(Hypertext Transfer Protocol over Secure Socket Layer,基于SSL的HTTP协议)使用了HTTP协议,但HTTPS使用不同于HTTP协议的 ...
- java的重写规则
重写不能破坏父类的访问性和逻辑结构性.对于异常重写方法不能抛出新的异常或者比被重写方法声明的检查异常更广的检查异常.但是可以抛出更少,更有限或者不抛出异常. 重写规则之一:重写方法不能比被重写方法限制 ...
- 【USACO 1.4】Mother's Milk
/* TASK: milk3 LANG: C++ SOLVE: 倒水,dfs,枚举每一种倒法,ca[i][j]记录a和c桶的状态,因为总体积不变,故b的状态不需要记录. */ #include< ...
- 【BZOJ-2599】Race 点分治
2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 2590 Solved: 769[Submit][Status ...
- CruiseControl.NET/CCNET配置(SVN+MSBuild+BAT+FTP)
CCNET目前最新版本为1.8.5,官方很久没更新过了,如果投入生成环境使用,建议全部转到Jenkins上. 可以直接在这里下载:http://www.cnblogs.com/EasonJim/p/5 ...
- oracle 锁的介绍 (转)
本文转自:http://blog.csdn.net/gyb2013/article/details/6929697 一.什么是锁: Oracle的锁机制是一种轻量级的锁定机制,不是通过构建锁列表来进行 ...
- ansible模块debug
示例: # Example that prints the loopback address and gateway for each host - debug: msg="System { ...