• Spring---ApplicationContext的事件机制


    事件源:ApplicationContext。publishEvent()方法:用于主动触发容器事件。
    
    事件:ApplicationEvent类,容器事件,必须由ApplicationContext发布。
    
    事件监听器:ApplicationListener接口,可由容器中任何监听器Bean担任。onApplicationEvent(ApplicationEvent event):每当容器内发生任何事件时,此方法都被触发

    容器事件类需继承ApplicationEvent类,容器事件的监听器类需实现ApplicationListener接口。

    下面给出一个例子

    1.首先定义一个EmailEvent类,继承ApplicationEvent类

    public class EmailEvent extends ApplicationEvent {
        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;
        }
    
        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,定义一个EmailNotifier类,实现ApplicationListener接口,并复写onApplicationEvent()方法

    public class EmailNotifier implements ApplicationListener {
        @Override
        public void onApplicationEvent(ApplicationEvent applicationEvent) {
            if (applicationEvent instanceof EmailEvent){
                EmailEvent emailEvent = (EmailEvent) applicationEvent;
                System.out.println("需要发送邮件的接收地址: " + emailEvent.getAddress());
                System.out.println("需要发送邮件的邮件正文: " + emailEvent.getText());
            }else {
                System.out.println("容器本身的事件: " + applicationEvent);
            }
        }
    }

    3.将监听器配置在容器中

    <bean class="org.spring.listener.EmailNotifier"/>

    4.编写主程序main

    public class SpringTest {
        public static void main(String[] args){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-config.xml");
    //        EmailEvent ele = new EmailEvent("hello","spring_test@163.com","this is a test");
    //        ctx.publishEvent(ele);
        }
    }

    程序到此结束,运行结果如下:

    容器本身的事件: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@b81eda8: startup date [Thu Dec 04 20:20:52 CST 2014]; root of context hierarchy]

    若取消main中的注释,即,使用publishEvent()来触发事件,运行结果如下:

    容器本身的事件: org.springframework.context.event.ContextRefreshedEvent[source=org.springframework.context.support.ClassPathXmlApplicationContext@b81eda8: startup date [Thu Dec 04 20:24:03 CST 2014]; root of context hierarchy]
    需要发送邮件的接收地址: spring_test@163.com
    需要发送邮件的邮件正文: this is a test
  • 相关阅读:
    详解threading模块:Condition类的使用
    concurrent.futures 模块使用说明
    细读Spring源码(四)什么是IOC?
    OAuth 2.0系列(六) OAuth令牌
    OAuth 2.0系列(七) OAuth 2.0可能存在的安全漏洞及预防方法
    细读Spring源码(一)refresh()方法概览
    HashMap源码解读() put方法的实现过程
    跟着字节码追踪JAVA中的基本运算
    细读Spring源码(六)Spring源码设计架构
    细读Spring源码(五)AOP从实战到源码
  • 原文地址:https://www.cnblogs.com/weilunhui/p/4143793.html
Copyright © 2020-2023  润新知