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()经过测试,能顺利实现既定目标。

Spring事件监听Demo的更多相关文章

  1. Spring事件监听机制源码解析

    Spring事件监听器使用 1.Spring事件监听体系包括三个组件:事件.事件监听器,事件广播器. 事件:定义事件类型和事件源,需要继承ApplicationEvent. package com.y ...

  2. spring事件监听(eventListener)

    原理:观察者模式 spring的事件监听有三个部分组成,事件(ApplicationEvent).监听器(ApplicationListener)和事件发布操作. 事件 事件类需要继承Applicat ...

  3. Spring 事件监听机制及原理分析

    简介 在JAVA体系中,有支持实现事件监听机制,在Spring 中也专门提供了一套事件机制的接口,方便我们实现.比如我们可以实现当用户注册后,给他发送一封邮件告诉他注册成功的一些信息,比如用户订阅的主 ...

  4. Spring事件监听ApplicationListener源码流程分析

    spring的事件机制是基于观察者设计模式的,ApplicationListener#onApplicationEvent(Event)方法,用于对事件的处理 .在容器初始化的时候执行注册到容器中的L ...

  5. Spring事件监听机制

    前言 Spring中的事件机制其实就是设计模式中的观察者模式,主要由以下角色构成: 事件 事件监听器(监听并处理事件) 事件发布者(发布事件) 首先看一下监听器和发布者的接口定义 public int ...

  6. Spring 事件监听

    Spring 的核心是 ApplicationContext,它负责管理 Bean的完整生命周期:当加载 Bean 时,ApplicationContext 发布某些类型的事件:例如,当上下文启动时, ...

  7. Spring之事件监听(观察者模型)

    目录 Spring事件监听 一.事件监听案例 1.事件类 2.事件监听类 3.事件发布者 4.配置文件中注册 5.测试 二.Spring中事件监听分析 1. Spring中事件监听的结构 2. 核心角 ...

  8. Spring的事件监听机制

    最近公司在重构广告系统,其中核心的打包功能由广告系统调用,即对apk打包的调用和打包完成之后的回调,需要提供相应的接口给广告系统.因此,为了将apk打包的核心流程和对接广告系统的业务解耦,利用了spr ...

  9. SpringBoot事件监听机制及观察者模式/发布订阅模式

    目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...

随机推荐

  1. 由select/epoll返回的非阻塞connect还会是EINPROGRESS状态吗?

    一般情况下,我们像下面代码中所示的这样使用非阻塞connect: #include <stdio.h> #include <stdlib.h> #include <str ...

  2. 给maven仓库添加镜像

    用maven下载好慢啊,快试试阿里云镜像吧!修改$MAVEN_HOME/conf/settings.xml <!-- 阿里云仓库 --> <mirror> <id> ...

  3. C# 如何调试安装包

    在需要调试的地方插入如下代码即可启动调试:  System.Diagnostics.Debugger.Launch();

  4. Python练习笔记——字符串反转

    请输入一段字符串,不利用反转函数,编写一段代码,将其反转. def list_reverse(a): list_long = len(a) list_long_half = list_long // ...

  5. OGG_Oracle GoldenGate简介(概念)

    2014-03-01 Created By BaoXinjian

  6. [置顶] Android中使用Movie显示gif动态图

    转载请注明:  http://blog.csdn.net/u012975705/article/details/48717391 在看这篇博文之前对attr自定义属性还是不是很熟的童鞋可以先看看:An ...

  7. http请求No peer certificate的解决方法

    不少同学在做HTTP请求新浪授权或新浪数据的时候会出现 javax.net.ssl.SSLPeerUnverifiedException: No peer certificate的异常.现给出解决方法 ...

  8. sklearn 中的交叉验证

    sklearn中的交叉验证(Cross-Validation) sklearn是利用python进行机器学习中一个非常全面和好用的第三方库,用过的都说好.今天主要记录一下sklearn中关于交叉验证的 ...

  9. InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings In

    InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is s ...

  10. 常用IP核

    前言 记录自己用到的模块,随时补充. 主要分类: 一.常用模块 1-FIFO FIFO分为两种,一是输入输出时钟相同(Common clock)的 fifo ;二是输入输出时钟不相同(Independ ...