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对事件的支持的更多相关文章

  1. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  2. Spring中ApplicationContext加载机制和配置初始化

    Spring中ApplicationContext加载机制.        加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet.        ...

  3. Spring中ApplicationContext和beanfactory区别---解析一

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

  4. Spring中ApplicationContext和beanfactory区别

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

  5. Spring中对资源的读取支持

    Resource简单介绍 注:所有操作基于配置好的Spring开发环境中. 在Spring中,最为核心的部分就是applicationContext.xml文件,而此配置文件中字符串的功能发挥到了极致 ...

  6. Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域

    //从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...

  7. Spring中ApplicationContext加载机制

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp33 加载器目前有两种选择:ContextLoaderListener和Co ...

  8. 关于Spring中applicationContext.xml配置错误“org/springframework/transaction/interceptor/TransactionInterceptor”的问题解决

    问题描述: 在配置spring的applicationContext.xml中的默认事务管理器的时候可能会出现这样的错误: Error occured processing XML 'org/spri ...

  9. 在Spring中使用异步事件实现同步事务

    结合Scala+Spring,我们将采取一个很简单的场景:下订单,然后发送一封电子邮件. 编制一个服务: @Serviceclass OrderService @Autowired() (orderD ...

随机推荐

  1. javascript 学习一(概述+基本语法)

    http://js.do/ 概述 JavaScript :脚本语言 诞生于1995年. javascript 1.0,发布于Netscape Navigator 2  @1996年3月 布兰登·艾奇( ...

  2. 概率 light oj 1104

    t个数据 n天一年  至少2个人在同一天生日的概率>=0.5 问至少多少人 显然要从反面考虑 设365天 都在不同一天的概率 p(num)=1*364/365*363/365...; =(day ...

  3. Echarts-柱状图柱图宽度设置

    先看两张图 图中柱图只需要设置series中的坐标系属性barWidth就可以, 这种图柱状图,折叠柱状图都适应 eg: /** * 堆积柱状图 * @param xaxisdata x轴:标签(数组 ...

  4. 【Codeforces 723C】Polycarp at the Radio 贪心

    n个数,用最少的次数来改变数字,使得1到m出现的次数的最小值最大.输出最小值和改变次数以及改变后的数组. 最小值最大一定是n/m,然后把可以改变的位置上的数变为需要的数. http://codefor ...

  5. webuploader跨域上传

    浏览器在跨域请求前会发个options请求来验证是否跨域,所以后端再处理这个options请求时,要告诉浏览器一些信息. 也就是个header信息 header("Access-Contro ...

  6. [poj2406] Power Strings

    Description 对于两个字符串a,b,定义a×b为将b接到a的末尾组成新的字符串.对于一个字符串a的幂运算的定义与我们在数学中的定义一样:a0=''(空字符),an+1=an×a. Input ...

  7. hdu 4609 FFT

    题意:给出一堆数,问从这些数中取3个能组成三角形的概率? sol:其实就是问从这些数里取3个组成三角形有多少种取法 脑洞大开的解法:用FFT 设一开始的数是1 3 3 4 作一个向量x,其中x[i]= ...

  8. HDU1150 Machine Schedule

    匈牙利算法 目前为止还是半懂不懂的状态 #include<iostream> #include<cstdio> #include<cstring> using na ...

  9. squid日志时间转换

    squid日志时间是unix时间戳 不利于观看 python脚本进行转换 #!/usr/bin/env python #2014-6-5 pdd import re import time impor ...

  10. LaTeX 笔记---Q&A

    Q: Is it possible to check which package do I need to install to get certain .sty file from tlmgr ra ...