1.介绍

用于监听应用程序事件的接口。

子接口:GenericApplicationListener,SmartApplicationListener。

通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理。

ApplicationContext事件机制是观察者设计模式的实现。

@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* 处理应用程序事件
*/
void onApplicationEvent(E event);
}

Spring内置事件:

ContextRefreshedEvent:ApplicationContext 被初始化或刷新时,该事件被发布。

@SuppressWarnings("serial")
public class ContextRefreshedEvent extends ApplicationContextEvent {
/**
* 创建一个新的ContextRefreshedEvent事件。
* 此时source已初始化或刷新
*/
public ContextRefreshedEvent(ApplicationContext source) {
super(source);
}
}

ContextStartedEvent:ApplicationContext启动时发布的广播事件。

ContextStoppedEvent:ApplicationContext停止时发布的广播事件。

ContextClosedEvent:ApplicationContext关闭时发布的广播事件。

SpringBoot内置事件:

ApplicationStartingEvent:系统运行开始时发布的广播事件,在进行任何处理之前发布广播。

@SuppressWarnings("serial")
public class ApplicationStartingEvent extends SpringApplicationEvent {
/**
* 创建一个新的ApplicationStartingEvent事件
* @param application 当前应用
* @param args 当前参数
*/
public ApplicationStartingEvent(SpringApplication application, String[] args) {
super(application, args);
}
}

ApplicationEnvironmentPreparedEvent:在系统环境准备完成创建上下文之前发布的广播事件。

ApplicationContextInitializedEvent:在完成所有初始化工作之后,加载任何Bean之前发布的广播事件。

ApplicationPreparedEvent:在加载bean definitions之后,容器刷新之前发布的广播事件。

ApplicationStartedEvent:在容器刷新之后,调用任何应用程序和命令程序之前发布的广播事件。

ApplicationReadyEvent:在调用任何应用程序和命令程序之后发布的广播事件。

ApplicationFailedEvent:在启动发生异常时发布的广播事件。

2.使用

自定义事件

@SuppressWarnings("serial")
public class CustomApplicationStartedEvent extends ApplicationContextEvent {
// 自定义参数
private final String msg; public CustomApplicationStartedEvent(ApplicationContext source,String msg) {
super(source);
this.msg = msg;
}
//getter方法
public String getMsg() {
return msg;
}
}

自定义监听

@Component
public class CustomApplicationListener implements ApplicationListener<CustomApplicationStartedEvent> {
@Override
public void onApplicationEvent(CustomApplicationStartedEvent event) {
System.out.println(event.getMsg());
ApplicationContext applicationContext = event.getApplicationContext();
// 打印容器里面有多少个bean
System.out.println("bean count=====" + applicationContext.getBeanDefinitionCount());
// 打印所有beanName
System.out.println(applicationContext.getBeanDefinitionCount() + "个Bean的名字如下:");
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for (String beanName : beanDefinitionNames) {
System.out.println("--------"+beanName);
}
}
}

发布广播事件

@SpringBootApplication
public class SsoApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SsoApplication .class);
ConfigurableApplicationContext run = application.run(args);
run.publishEvent(new CustomApplicationStartedEvent(run,"自定义监听事件"));
}
}

Spring扩展之二:ApplicationListener的更多相关文章

  1. spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情

    <spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...

  2. spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入

    <spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...

  3. Spring IOC(二)容器初始化

    本系列目录: Spring IOC(一)概览 Spring IOC(二)容器初始化 Spring IOC(三)依赖注入 Spring IOC(四)总结 目录 一.ApplicationContext接 ...

  4. spring AOP 之二:@AspectJ注解的3种配置

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  5. Spring ApplicationContext(二)环境准备

    Spring ApplicationContext(二)环境准备 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 本节介绍 ...

  6. Spring入门(二)— IOC注解、Spring测试、AOP入门

    一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...

  7. Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面

    Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面 在<Spring Boot(一):快速开始>中介绍了如何使用 Spring Boot 构建一个工程,并且提 ...

  8. Spring Security 解析(二) —— 认证过程

    Spring Security 解析(二) -- 认证过程   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security .S ...

  9. spring扩展点整理

    本文转载自spring扩展点整理 背景 Spring的强大和灵活性不用再强调了.而灵活性就是通过一系列的扩展点来实现的,这些扩展点给应用程序提供了参与Spring容器创建的过程,好多定制化的东西都需要 ...

随机推荐

  1. js 判断客户端 和 asp.net/C#判断客户端类型

    1.js 判断客户端 <script language="JavaScript"> <!-- onload = function browserRedirect( ...

  2. Cypress系列(67)- 环境变量设置指南

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 常见的环境变量设置方式 可参考这篇文章: ...

  3. Vulkan Driver for VC4(Raspberry Pi 3b) base on mesa

    这是一篇关于在raspberry Pi 3b上移植实现vulkan 驱动的文章. 经过一段时间的代码搬运,终于实现了零的突破,可以在树莓派3B上运行Vulkan triangle/texture.当然 ...

  4. Spring Boot学习笔记(二)——HelloWorld实现

    提示:要在Eclipse里使用Spring Boot,首先要安装STS插件,前面我们已经安装了STS插件了,可以创建Spring Boot项目了. 1.创建项目: 新建项目,选择Spring Boot ...

  5. MongoDB分片 --- MongoDB基础用法(六)

    分片 在Mongodb里面存在另一种集群,就是分片技术,可以满足MongoDB数据量大量增长的需求. 当MongoDB存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量. ...

  6. 【总结】HTTP

    一.HTTP 1.http HTTP 是一种 超文本传输协议(Hypertext Transfer Protocol),HTTP 是一个在计算机世界里专门在两点之间传输文字.图片.音频.视频等超文本数 ...

  7. django 框架模型之models常用的Field

    1. django 模型models 常用字段          1.models.AutoField 自增列 = int(11) 如果没有的话,默认会生成一个名称为 id 的列 如果要显式的自定义一 ...

  8. MIPS汇编及模拟器下载

    1. 简述汇编语言发展 在计算机发展初期,人们用0-1序列来表示每一条语言,亦即二进制的机器指令 由于机器指令过于繁琐,程序员们开发出了一种新的语言,这种用符号表示的计算机语言被称为汇编语言 计算机继 ...

  9. AQS源码深入分析之共享模式-你知道为什么AQS中要有PROPAGATE这个状态吗?

    本文基于JDK-8u261源码分析 本篇文章为AQS系列文的第二篇,前文请看:[传送门] 第一篇:AQS源码深入分析之独占模式-ReentrantLock锁特性详解 1 Semaphore概览 共享模 ...

  10. 致萌新与不会用 NOI Linux 的 OIer

    全文绝大部分转载自:这篇好文章啊. 目录 1:GUIDE 2:Gedit 原文 打开 编译运行 3.Vim 3-1:这东西咋开啊 3-2:这东西咋用啊 4.编译与运行 5.调试 6.CSP竞赛中编写代 ...