Spring中ApplicationContext的事件机制

ApplicationContext事件机制是观察者设计模式的 实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理。 如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发。
Spring的事件框架有如下两个重要的成员:
ApplicationEvent:容器事件,必须由ApplicationContext发布;
ApplicationListener:监听器,可由容器中的任何监听器Bean担任。
实际上,Spring的事件机制与所有时间机制都基本相似,它们都需要事件源、事件和事件监听器组成。只是此处的事件源是 ApplicationContext,且事件必须由Java程序显式触发。下面的程序将演示Spring容器的事件机制。
Spring框架事先定义了一个 ApplicationEvent类,其对象就是一个Spring容器事件,其源码中的解释是:Class to be extended by all application events. Abstract as itdoesn't make sense for generic events to be published directly.

代码如下:

package com.cxg.test.springPlatfrom;

import org.springframework.context.ApplicationEvent;
/**
 * Title: email之事件类
 * EmailEvent类继承了ApplicationEvent类,除此之外,它就是一个普通的Java类
 * Description: dataPlatfrom
 * @author: xg.chen
 * @date:2016年8月24日
 */
public class EmailEvent extends ApplicationEvent{
    private static final long serialVersionUID = 1L;
    //属性
    private String address;
    private String text;
    //构造方法
    public EmailEvent(Object source) {
        super(source);
    }
    public EmailEvent(Object source, String address, String text) {
        super(source);
        this.address = address;
        this.text = text;
    }
    //getter和setter设置
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
}
上面的EmailEvent类继承了ApplicationEvent类,除此之外,它就是一个普通的Java类。
    容器事件的监听器类必须实现ApplicationListener接口,实现该接口就必须实现如下方法:onApplicationEvent(ApplicationEvent event):每当容器内发生任何事件时,此方法都会被触发。在Spring源码中给出的解释是:Interface to be implemented by application event listeners. Based on the standard {@code java.util.EventListener} interface for the Observer design pattern.

容器监听器类代码如下:

package com.cxg.test.springPlatfrom;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/**
 * Title: email之监听类
 * 容器事件的监听器类必须实现ApplicationListener接口,实现该接口就必须实现
 * Description: dataPlatfrom
 * @author: xg.chen
 * @date:2016年8月24日
 */
public class EmailNotifier implements ApplicationListener<ApplicationEvent>{

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if(event instanceof EmailEvent){
            EmailEvent emailEvent = (EmailEvent) event;
            System.out.println("email's address:"+emailEvent.getAddress());
            System.out.println("email's text:"+emailEvent.getText());
        } else {
            System.out.println("the Spring's event:"+event);
        }
    }

}

将监听器配置在Spring的容器中,代码如下:

<!-- 配置事件监听 -->
    <bean class="com.cxg.test.springPlatfrom.EmailNotifier" />
为Spring容器注册监听器,不需要像AWT编程那样采用代码进行注册,只需要在Spring的配置文件中进行简单配置即可。当我们住唉 Spring中配置了一个实现了ApplicationListener的Bean,Spring容器就会把这个Bean当成容器事件的监听器。当系统创建Spring容器、加载Spring容器时会自动触发容器事件,容器事件监听器可以监听到这些事件。除此之外,程序也可以调用ApplicationContext的publishEvent()方法来主动触发一个容器事件。

测试代码如下:

package com.cxg.test.springPlatfrom;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Title: Spring的ApplicationContexet单元成测试
 * Description: dataPlatfrom
 * @author: xg.chen
 * @date:2016年8月24日
 */
public class SpringTest {
    public static void main(String arg[]){
        //读取Spring容器的配置文件
        @SuppressWarnings("resource")
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
        //创建一个事件对象
        EmailEvent emailEvent = new EmailEvent("hello Spring!", "cxg@126.com", "This is SpringApplicatoinContext test!");
        //主动触发事件监视机制
        applicationContext.publishEvent(emailEvent);
    }
}

运行输出:

email's address:cxg@126.com
email's text:This is SpringApplicatoinContext test!
注意:如果Bean想发布事件,则Bean必须获得其容器的引用。如果程序中没有直接获取容器的引用,则应该让Bean实现ApplicationContextAware或者BeanFactoryAware接口,从而可以获得容器的引用。
Spring提供如下几个内置事件:
ContextRefreshedEvent:ApplicationContext容器初始化或刷新时触发该事件。此处的初始化是指:所有的 Bean被成功装载,后处理Bean被检测并激活,所有Singleton Bean 被预实例化,ApplicationContext容器已就绪可用。
ContextStartedEvent:当使用 ConfigurableApplicationContext(ApplicationContext的子接口)接口的start()方法启动 ApplicationContext容器时触发该事件。容器管理声明周期的Bean实例将获得一个指定的启动信号,这在经常需要停止后重新启动的场合比 较常见。
ContextClosedEvent:当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext时触发该事件。
ContextStoppedEvent:当使用ConfigurableApplicationContext接口的stop()方法使 ApplicationContext容器停止时触发该事件。此处的停止,意味着容器管理生命周期的Bean实例将获得一个指定的停止信号,被停止的 Spring容器可再次调用start()方法重新启动。
RequestHandledEvent:Web相关事件,只能应用于使用DispatcherServlet的Web应用。在使用Spring作为前端的MVC控制器时,当Spring处理用户请求结束后,系统会自动触发该事件。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值