• Spring的ApplicationEvent实现


    原理:ApplicationContextAware接口提供了publishEvent方法,实现了Observe(观察者)设计模式的传播机制,实现了对bean的传播。通过ApplicationContextAware我们可以把系统中所有ApplicationEvent传播给系统中所有的ApplicationListener。

    直接上代码:

    1.定义自己的监听事件

    2.定义自己的监听器(负责处理自己的监听事件)

    3.定义一个bean触发监听事件

    4.测试

    package com.test.eventListener;
    
    import org.springframework.context.ApplicationEvent;
    
    /**
     * @author admin
     * @date 2018/5/17 17:37
     * 新建StudentAddEvent类,实现抽象类org.springframework.context.ApplicationEvent
     * StudentAddEvent类中需要实现自己的构造函数
     *  增加学生监听事件
     */
    public class StudentAddEvent extends ApplicationEvent {
    
        private static final long serialVersionUID = 20L;
    
        /**
         * 学生姓名
         */
        private String name;
    
        /**
         * @param source
         */
        public StudentAddEvent(Object source, String name) {
            super(source);
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    package com.test.eventListener;
    
    import org.springframework.context.ApplicationEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
    
    /**
     * @author admin
     * @date 2018/5/17 17:41
     * 新建StudentAddListener类,实现接口org.springframework.context.ApplicationListener中的onApplicationEvent方法,
     * 在该方法中只处理StudentAddEvent类型的ApplicationEvent事件
     * 定义StudentAddListener监听器
     */
    @Component
    public class StudentAddListener implements ApplicationListener {
    
        public void onApplicationEvent(ApplicationEvent event) {
            // 1.判断是否是增加学生对象的事件
            if (!(event instanceof StudentAddEvent)) {
                return;
            }
    
            // 2.是增加学生事件的对象,进行逻辑处理,比如记日志、积分等
            StudentAddEvent studentAddEvent = (StudentAddEvent) event;
            System.out.println("增加了学生:" + studentAddEvent.getName());
        }
    }
    package com.test.eventListener;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    /**
     * @author admin
     * @date 2018/5/17 17:45
     * 定义StudentAddBean触发StudentAddEvent事件
     * 新建StudentAddBean类,实现接口 org.springframework.context.ApplicationContextAware中的setApplicationContext方法,
     * 在构造bean的时候注入Spring的上下文对象,以便通过Spring上下文对象的publishEvent方法来触发StudentAddEvent事件
     */
    @Component
    public class StudentAddBean implements ApplicationContextAware {
        /**
         * 定义Spring上下文对象
         */
        private ApplicationContext applicationContext = null;
    
        /*
         * (non-Javadoc)
         *
         * @see
         * org.springframework.context.ApplicationContextAware#setApplicationContext
         * (org.springframework.context.ApplicationContext)
         */
        public void setApplicationContext(ApplicationContext applicationContext)
                throws BeansException {
            this.applicationContext = applicationContext;
    
        }
    
        /**
         * 增加一个学生
         *
         * @param studentName
         */
        public void addStudent(String studentName) {
            // 1.构造一个增加学生的事件
            StudentAddEvent aStudentEvent = new StudentAddEvent(
                    applicationContext, studentName);
            // 2.触发增加学生事件
            applicationContext.publishEvent(aStudentEvent);
        }
    
    }
    package com.test.eventListener;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author admin
     * @date 2018/5/17 17:55
     * ApplicationContext在运行期会自动检测到所有实现了ApplicationListener的bean对象,并将其作为事件接收对象。
     * 当ApplicationContext的publishEvent方法被触发时,每个实现了ApplicationListener接口的bean都会收到ApplicationEvent对象,
     * 每个ApplicationListener可根据事件类型只接收处理自己感兴趣的事件,比如上面的StudentAddListener只接收StudentAddEvent事件。
     */
    public class EventListenerTest {
        public static void main(String[] args) {
            String[] xmlConfig = new String[] { "spring/spring.xml" };
            // 使用ApplicationContext来初始化系统
            ApplicationContext context = new ClassPathXmlApplicationContext(xmlConfig);
            StudentAddBean studentBean = (StudentAddBean) context.getBean("studentAddBean");
            studentBean.addStudent("张三");
            studentBean.addStudent("李四");
        }
    }
  • 相关阅读:
    SQL创建索引
    SQLServer最耗资源时间的SQL语句
    C# Linq删除父级的同时删除子级
    C# 根据类名创建类的实例对象
    C#利用反射实现两个类的对象之间相同属性的值的复制
    linq时间筛选以及list时间筛选
    为什么watch机制不是银弹?
    我们的前端模版引擎更新总结
    小矮人Javascript模块加载器
    Javascript模版引擎简介
  • 原文地址:https://www.cnblogs.com/xubiao/p/9052440.html
Copyright © 2020-2023  润新知