使用方法

spring监听模式需要三个组件:

1. 事件,需要继承ApplicationEvent,即观察者模式中的"主题",可以看做一个普通的bean类,用于保存在事件监听器的业务逻辑中需要的一些字段;

2. 事件监听器,需要实现ApplicationListener<E extends ApplicationEvent>,即观察者模式中的"观察者",在主题发生变化时收到通知,并作出相应的更新,加泛型表示只监听某种类型的事件;

3. 事件发布器,需要实现ApplicationEventPublisherAware,获取spring底层组件ApplicationEventPublisher,并调用其方法发布事件,即"通知"观察者。

其中,事件监听器和事件发布器需要在springIOC容器中注册。

示例Demo

事件类

import org.springframework.context.ApplicationEvent;

/**
* spring监听机制中的"事件"
* created on 2019-04-15
*/
public class BusinessEvent extends ApplicationEvent { //事件的类型
private String type; /**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
* 即事件是在哪个对象上发生的
*/
public BusinessEvent(Object source, String type) {
super(source);
this.type = type;
} public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}

事件监听器

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* spring监听机制中"监听器"
* created on 2019-04-15
*/
@Component
public class BusinessListener implements ApplicationListener<BusinessEvent> { /**
* 监听到事件后做的处理
* @param event
*/
@Override
public void onApplicationEvent(BusinessEvent event) {
System.out.println("监听到事件:" + event.getType());
}
}

事件发布器

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component; /**
* spring事件监听机制中的"事件发布器"
* created on 2019-04-15
*/
@Component
public class BusinessPublisher implements ApplicationEventPublisherAware { //spring提供的事件发布组件
private ApplicationEventPublisher applicationEventPublisher; @Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
} /**
* 发布事件
*/
public void publishEvent(BusinessEvent businessEvent) {
System.out.println("发布事件:" + businessEvent.getType());
this.applicationEventPublisher.publishEvent(businessEvent);
}
}

容器配置类

/**
* spring容器配置类
* 需要在容器中注册事件监听器、事件发布器
* created on 2019-04-15
*/
@ComponentScan(basePackages = {"cn.monolog.bennett.observer.event.listener"})
public class BeanConfig {
}

测试类

/**
* 用于测试spring事件监听
* created on 2019-04-15
*/
public class Test { public static void main(String[] args) {
//创建springIOC容器
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
//从容器中获取事件发布器实例
BusinessPublisher businessPublisher = applicationContext.getBean(BusinessPublisher.class);
//创建事件
BusinessEvent businessEvent = new BusinessEvent(new Test(), BusinessType.ALLOT.getName());
//发布事件
businessPublisher.publishEvent(businessEvent);
}
}

源码分析

在观察者模式中,主题发生改变时,会"通知"观察者作出相应的操作,实现方式是获取观察者列表,然后遍历、分别执行一遍其更新方法。那么,在spring事件监听中,事件发生变化时,是如何"通知"到观察者的呢?如上面的demo所述,我们是通过spring的组件ApplicationEventListener接口执行publishEvent方法发布事件的,而这个抽象方法在spring中只有一个实现,就是AbstractrApplicationContext,这是一个容器类。我们来跟进一下这个容器类对于发布事件的实现方法源码:

    protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
Assert.notNull(event, "Event must not be null"); // Decorate event as an ApplicationEvent if necessary
ApplicationEvent applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
else {
applicationEvent = new PayloadApplicationEvent<>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
}
} // Multicast right now if possible - or lazily once the multicaster is initialized
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
else {
       //获取事件广播器、然后广播事件
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
} // Publish event via parent context as well...
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
}
else {
this.parent.publishEvent(event);
}
}
}

粗体部分语句:首先获取事件广播器、然后广播事件。

所以问题分为两部分:如何获取事件广播器、怎样广播事件。

1. 获取事件广播器

直接跟进上述语句——getApplicationEventMulticaster(),似乎找不到答案,因为这个方法是直接返回了AbstractApplicationContext类的属性。问题转化为:AbstractApplicationContext类中的事件广播器属性是什么时候被赋值的?这就要从容器创建说起了。springIOC容器创建有一个重要步骤——刷新容器refresh(),就是在AbstractApplicationContext中定义的,这个refresh()中包含了容器创建、初始化的诸多操作,其中两个步骤与事件监听有关,看一下源码

    public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh(); // Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory); try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory); // Initialize message source for this context.
initMessageSource(); // Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh(); // Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event.
finishRefresh();
} catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
} // Destroy already created singletons to avoid dangling resources.
destroyBeans(); // Reset 'active' flag.
cancelRefresh(ex); // Propagate exception to caller.
throw ex;
} finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}

第一个步骤是initApplicationEventMulticaster,即初始化事件广播器,继续跟进源码会发现,是先从BeanFactory中获取,如果不存在,就新建一个。第二个步骤是registerListeners,即注册监听器,从容器中获取所有ApplicationEventListener类型的组件,添加进事件广播器。

2. 广播事件

广播事件的方法是写在事件广播器的实现类——SimpleApplicationEventMulticater中的。

@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
   //遍历监听器,分别执行invokeListener
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}

从源码中可以看出,SimpleApplicationEventMulticater从容器中获取所有的监听器列表,遍历列表,对每个监听器分别执行invokeListener方法,继续跟进invokeListener方法,它会调用一个doInvokeListener,在这个doInvokeListner中:

@SuppressWarnings({"unchecked", "rawtypes"})
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
//调用监听器实现类的onApplicationEvent方法
listener.onApplicationEvent(event);
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type for
// -> let's suppress the exception and just log a debug message.
Log logger = LogFactory.getLog(getClass());
if (logger.isDebugEnabled()) {
logger.debug("Non-matching event type for listener: " + listener, ex);
}
}
else {
throw ex;
}
}
}

终于看到我们熟悉的:onApplicationEvent方法,这就是暴露在外层、供我们使用的事件监听方法;

也就是在这里,实现了观察者模式中的——"通知"观察者进行更新的操作。

spring监听机制——观察者模式的应用的更多相关文章

  1. 深入理解Spring的容器内事件发布监听机制

    目录 1. 什么是事件监听机制 2. JDK中对事件监听机制的支持 2.1 基于JDK实现对任务执行结果的监听 3.Spring容器对事件监听机制的支持 3.1 基于Spring实现对任务执行结果的监 ...

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

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

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

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

  4. Spring ApplicationContext(八)事件监听机制

    Spring ApplicationContext(八)事件监听机制 本节则重点关注的是 Spring 的事件监听机制,主要是第 8 步:多播器注册:第 10 步:事件注册. public void ...

  5. 【spring源码学习】spring的事件发布监听机制源码解析

    [一]相关源代码类 (1)spring的事件发布监听机制的核心管理类:org.springframework.context.event.SimpleApplicationEventMulticast ...

  6. Spring笔记(7) - Spring的事件和监听机制

    一.背景 事件机制作为一种编程机制,在很多开发语言中都提供了支持,同时许多开源框架的设计中都使用了事件机制,比如SpringFramework. 在 Java 语言中,Java 的事件机制参与者有3种 ...

  7. Spring事件发布与监听机制

    我是陈皮,一个在互联网 Coding 的 ITer,微信搜索「陈皮的JavaLib」第一时间阅读最新文章,回复[资料],即可获得我精心整理的技术资料,电子书籍,一线大厂面试资料和优秀简历模板. 目录 ...

  8. Laravel 事件系统用法总结(监听事件,观察者模式)

    看这篇文章先复习一下设计模式 : https://www.cnblogs.com/fps2tao/p/9640338.html 在理解了观察者模式后,我们开始正文 Laravel 的事件提供了一个简单 ...

  9. 7_3.springboot2.x启动配置原理_3.事件监听机制

    事件监听机制配置在META-INF/spring.factories ApplicationContextInitializer SpringApplicationRunListenerioc容器中的 ...

随机推荐

  1. 05-Django-session-admin

    # session- 为了应对HTTP协议的无状态性- 用来保存用户比较敏感的信息- 属于request的一个属性- 常用操作: - request.session.get(key, defaultV ...

  2. SpringBoot_04springDataJPA

    说明:底层使用Hibernate 一.springDataJPA和mybatisPlus的使用区别 第一步: 把mybatisPlus的依赖.配置删除 包括:实体类的注解.引导类的mapperScan ...

  3. axios 文件流下载

    this.axios .post(this.baseUrl+"/exportUser", { admin: "",keys: "",keyw ...

  4. freemarker如何在url中传递中文参数

    例如:http://www.map512.cn/findPOI.do?key=南门如果不转码,request.getParameter("key")返回的是乱码,在jsp中,我们一 ...

  5. Linux学习--第三天--linux文件目录、ls、mkdir、mv、rm、touch、cat、tac、more、less、head、tail、ln、chmod、chown、chgrp、umask

    文件目录 目录名 备注 bin 下面的命令所有人都可以运行 sbin 只有root才能运行,s代表super /mnt,/media,/misc 都是挂载目录,但一般只用mnt /opt 第三方软件安 ...

  6. Cobbler自动化装机

    Cobbler自动化装机 一个可以实现批量安装系统的Linxu应用程序,他可以实现同个服务器安装不同操作系统版本. 准备环境 开启两个网卡.一个仅主机模式,一个桥接模式,仅主机模式对内提供cobble ...

  7. Uva 10635 - Prince and Princess LCS/LIS

    两个长度分别为p+1和q+1的由1到n2之前的整数组成的序列,每个序列的元素各不相等,两个序列第一个元素均为1.求两个序列的最长公共子序列 https://uva.onlinejudge.org/in ...

  8. C#基础知识之System.AppDomain类

    进程是存在独立的内存和资源的,但是AppDomain仅仅是逻辑上的一种抽象.一个process可以存在多个AppDomain.各个AppDomain之间的数据时相互独立的.一个线程可以穿梭多个AppD ...

  9. Python(3) 进制转换

    2进制 :0b8进制: 0o16进制: 0x10进制:原来的数据 进制转换:bin() 方法:转化为 2进制 >>> bin(10)'0b1010'oct() 方法:转化为 8进制& ...

  10. jmeter测试https请求之导入证书

    jmeter测试https请求   公司最近在搞全站HTTPS改造,进一步提高网站的安全性,防止运营商劫持.那么,改造完成后,所有前后端的URL将全部为https. So ,研究下怎么用Jmeter访 ...