博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring事件监听Demo
阅读量:6925 次
发布时间:2019-06-27

本文共 3249 字,大约阅读时间需要 10 分钟。

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")@Componentpublic class StudentAddListenerTest implements ApplicationListener
{ @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")@Componentpublic 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()经过测试,能顺利实现既定目标。

转载地址:http://imcjl.baihongyu.com/

你可能感兴趣的文章
数据的序列化
查看>>
姚氏百万富翁问题数值解法“非电路解法”详解(python代码实现,原理分析)...
查看>>
【REACT NATIVE 系列教程之十】真机运行报错COMMAND /BIN/SH FAILED WITH EXIT CODE 1 的解决方法...
查看>>
C++解逆波兰表达式
查看>>
Python 去除列表中重复的元素
查看>>
Memcached深入剖析
查看>>
接口的意义
查看>>
nagios监控远程主机上的资源-cpu
查看>>
不要“死”在“最后一问”
查看>>
算法导论Java实现-MAX-HEAPIFY算法(6.2章节)
查看>>
Spring远程访问技术 例子
查看>>
banq、J道——相见恨晚!
查看>>
Android SDK的docs访问速度很慢
查看>>
胡烁学记:技术开发行业7种典型客户的谈单应对技巧
查看>>
JSON简单例子
查看>>
初学安全
查看>>
es SynonymTokenFilterFactory 源码
查看>>
dubbo -搭建监控中心
查看>>
mysql日志管理
查看>>
k8s dashboard 认证配置
查看>>