• Spring事件监听Demo


    Spring事件监听实现了观察者模式。本Demo在junit4测试环境中实现

    主要有三个类事件类、监听器类、事件发布类(入口)

    事件类必须继承 ApplicationEvent,代码如下:

    import org.junit.runner.RunWith;
    import org.springframework.context.ApplicationEvent;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    /**
     * Created by *** on 2016/3/15.
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml")
    public class StudentAddEventTest extends ApplicationEvent {
    
        private String sname;
    
        public StudentAddEventTest(Object source, String sname) {
            super(source);
            this.sname = sname;
        }
    
        public String getSname() {
            return sname;
        }
    }

    监听器Listener类需实现  ApplicationListener 接口  ApplicationListener可以传入一个泛型的事件类型变量,也可以不传。不传的话,需要在监听到事件发生时(onApplicationEvent)自己判断该事件是不是自己要监听的事件。

    import org.junit.runner.RunWith;
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    /**
     * Created by *** on 2016/3/15.
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml")
    @Component
    public class StudentAddListenerTest implements ApplicationListener<StudentAddEventTest> {
    
        @Override
        public void onApplicationEvent(StudentAddEventTest studentAddEventTest) {
            String sname = studentAddEventTest.getSname();
            System.out.println("增加的学生的名字为:::"+sname);
        }
    }

    事件发布类实现了 ApplicationContextAware ,实现这个主要是为了拿到 ApplicationContext实例,进而调用他的 publishEvent方法。感觉不实现ApplicationContextAware应该也可以。。。

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    /**
     * Created by *** on 2016/3/15.
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:applicationContext-indexConfig-test.xml")
    @Component
    public class StudentAddBeanTest implements ApplicationContextAware {
        private static ApplicationContext applicationContext;
    
        @Override
        @Autowired
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext  = applicationContext;
        }
    
        public void addStudent(String sname){
            StudentAddEventTest addEvent = new StudentAddEventTest(this, sname);
            applicationContext.publishEvent(addEvent);
        }
    
        @Test
        public void addTest(){
            StudentAddBeanTest studentBean = new StudentAddBeanTest();
            studentBean.addStudent("张三");
            studentBean.addStudent("李四");
        }
    
    }

    需要注意的是 StudentAddListenerTest ——Listener类,StudentAddBeanTest ——事件发布类需在spring容器中注册,Demo中在 applicationContext-indexConfig-test.xml配置了

    扫描<context:component-scan> </context:component-scan>路径

    然后在类上加了注解@Component,运行测试方法addTest()经过测试,能顺利实现既定目标。

  • 相关阅读:
    命令练习题2
    l命令练习题1
    命令用法习题,yum仓库的创建 chapter02
    网络基础知识
    Linux常用的命令及使用方法
    Linux 常见的常识及常用快捷键方式
    一条命令解决mac版本python IDLE无法输入中文问题
    RS232串口的Windows编程纪要
    在龙芯小本上安装Debain8.10
    mac电脑进行可见光通信实验要点
  • 原文地址:https://www.cnblogs.com/winkey4986/p/5279502.html
Copyright © 2020-2023  润新知