ApplicationContext的事件机制

来源:互联网 发布:淘宝借钱逾期 编辑:程序博客网 时间:2024/05/17 08:42

一、概述
applicationContext的事件机制是观察者模式实现的,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext的事件处理。

  1. ApplicationEvent:容器事件,必须由ApplicationContext发布
  2. ApplicationListener:监听器,可由容器中任何监听器Bean担任
    事件机制一般由事件源、事件、监听器组成
    二、简单示例代码说明

1.容器事件

package com.event;import org.springframework.context.ApplicationEvent;/** * 继承了ApplicationEvent,那该对象可作为Spring容器对象 */public class EmailEvent  extends ApplicationEvent{    private String address;    private String text;    public EmailEvent(Object source) {        super(source);        // TODO Auto-generated constructor stub    }    public EmailEvent(Object source,String address,String text){        super(source);//调用父类方法        this.address=address;        this.text=text;    }    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;    }}

2.容器事件监听器

package com.event;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationListener;public class EmialNotifier implements ApplicationListener<ApplicationEvent>{    public void onApplicationEvent(ApplicationEvent event) {        // TODO Auto-generated method stub        if(event instanceof EmailEvent){//instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例            EmailEvent emailEvent=(EmailEvent) event;            System.out.println("需要发送邮件的接收地址为:"+emailEvent.getAddress());            System.out.println("需要发送邮件的正文为:"+emailEvent.getText());        }else{            //其他时间不做任何处理            System.out.println("其他事件:"+event);        }    }}

3.配置文件

<!-- 配置事件监听器 --><bean class="com.event.EmialNotifier"/>

4.测试代码

package com.event;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class EventTest {    public static void main(String[] args) {        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");        //创建一个applicationEvent对象        EmailEvent emailEvent=new EmailEvent("text", "广东省茂名市", "spring_event@qq.com");        //发布事件        ctx.publishEvent(emailEvent);    }}输出如下结果:其他事件:org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@78105738: startup date [Tue Aug 29 23:45:15 CST 2017]; root of context hierarchy]需要发送邮件的接收地址为:广东省茂名市需要发送邮件的正文为:spring_event@qq.com由此可以看出,监听器不仅能够监听程序所触发的事件,也能监听到内置的事件

四、内置事件简述

package com.bjhy.platform.supermarket.test;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;/** * Spring boot 启动容器加载完成之后,做一系列的事情可以实现ApplicationListener * Spring的内置事件:  * 1.ContextRefreshedEvent:ApplicationContext容器初始化或者刷新时触发该事件。 * 2、 ContextStartedEvent:当使用ConfigurableApplicationContext接口的start()方法启动    ApplicationContext容器时触发该事件。    3、 ContextClosedEvent:当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext容器时触发该事件。    4、 ContextStopedEvent: 当使用ConfigurableApplicationContext接口的stop()方法停止ApplicationContext容器时触发该事件。    5.RequestHandleEvent:Web相关的事件,只能应用于使用DispatherServlet的Web中,在使用Spring作为前端的MVC控制器时,当Spring处理用户请求结束后,系统会触发改事件 * * */public class ApplicationRequestHandleEvent implements ApplicationListener<ContextRefreshedEvent> {    @Override    public void onApplicationEvent(ContextRefreshedEvent event) {        ApplicationContext context = event.getApplicationContext();        //执行操作code    }}
原创粉丝点击