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. Web Service基础——四种客户端调用方式

    通过访问公网服务地址 http://www.webxml.com.cn/zh_cn/index.aspx 来演示四种不同的客户端调用方式 1. 生成客户端调用方式 1.1 Wsimport命令介绍 首 ...

  2. 用 python 爬虫抓站的一些技巧总结

    学用python也有3个多月了,用得最多的还是各类爬虫脚本:写过抓代理本机验证的脚本,写过在discuz论坛中自动登录自动发贴的脚本,写过自动收邮件的脚本,写过简单的验证码识别的脚本,本来想写goog ...

  3. 为Github 托管项目的访问添加SSH keys

    为了便于访问远程仓库,各个协作者将自己的本地的项目内容推送到远程仓库中,使用 SSH keys 验证github的好处:不用每次提交代码时都输入用户名和密码. 如果SSH key没有添加到github ...

  4. 【C++】const成员函数

    形式: 在成员函数后面加上const限定词,表示不会修改对象内容. 例如Circle类: class Circle { double r; public: Circle(double newr) { ...

  5. 从使用 KVO 监听 readonly 属性说起

    01.KVO 原理 KVO 是 key-value observing 的简写,它的原理大致是: 1.当一个 object(对象) 有观察者时候,动态创建这个 object(对象) 的类的子类(以 N ...

  6. Java生成唯一GUID

    GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...

  7. 《JAVA与模式》之适配器模式(转载)

    适配器模式比较简单,偷个懒,直接转载一篇. 个人理解: * 类适配器是通过继承来完成适配 * 对象适配器是通过传递对象来完成适配 * 不管哪种,其实都是通过引用特殊接口的对象来完成特殊接口的适配调用 ...

  8. 关系数据库元数据处理类(一) 创建MSSQL元数据具体处理类

    public class SqlServer : BaseMetadata { public SqlServer(string connectionString) : base(new DbUtili ...

  9. Axure多人协作

    这几天搞<材料採购系统>需求.我们组须要分模块画模型图,可是假设每一个人各自画各自的,最后整合,这样就太麻烦了.小左说Axure能够实现多人协作,于是我就研究了一下.我们组已经在用了.以下 ...

  10. 【Life】 Never Too Late, Just Do it Better!

    开这个博客: 一来是认为自己记忆力不好,对所学的东西做个记录: 二来是希望找到很多其它志同道合的人.一起交流进步: 不论什么时候開始努力都不晚! 希望平淡的工作生活不要磨灭我们心中的梦想,与君共勉~