Spring扩展之二:ApplicationListener
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的更多相关文章
- spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情
<spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...
- spring扩展点之二:spring中关于bean初始化、销毁等使用汇总,ApplicationContextAware将ApplicationContext注入
<spring扩展点之二:spring中关于bean初始化.销毁等使用汇总,ApplicationContextAware将ApplicationContext注入> <spring ...
- Spring IOC(二)容器初始化
本系列目录: Spring IOC(一)概览 Spring IOC(二)容器初始化 Spring IOC(三)依赖注入 Spring IOC(四)总结 目录 一.ApplicationContext接 ...
- spring AOP 之二:@AspectJ注解的3种配置
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
- Spring ApplicationContext(二)环境准备
Spring ApplicationContext(二)环境准备 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 本节介绍 ...
- Spring入门(二)— IOC注解、Spring测试、AOP入门
一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...
- Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面
Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面 在<Spring Boot(一):快速开始>中介绍了如何使用 Spring Boot 构建一个工程,并且提 ...
- Spring Security 解析(二) —— 认证过程
Spring Security 解析(二) -- 认证过程 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security .S ...
- spring扩展点整理
本文转载自spring扩展点整理 背景 Spring的强大和灵活性不用再强调了.而灵活性就是通过一系列的扩展点来实现的,这些扩展点给应用程序提供了参与Spring容器创建的过程,好多定制化的东西都需要 ...
随机推荐
- xpath教程-通过ID和Class检索 转
通过ID和Class检索 必备知识点 在html中,id是唯一的 在html中,class是可以多处引用的 工具 Python3版本 lxml库[优点是解析快] HTML代码块[从网络中获取或者自 ...
- Seaborn系列 | 散点图scatterplot()
散点图 解读 可以通过调整颜色.大小和样式等参数来显示数据之间的关系. 函数原型 seaborn.scatterplot(x=None, y=None, hue=None, style=None, s ...
- Redis入门之认识redis(一)
第1章 非关系型数据库 1.1 NoSQL数据库概述 1) NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL",泛指非关系型的数据库. NoSQL 不 ...
- leaflet实现台风动态轨迹
leaflet平台是我最新使用的webGIS平台,该平台比较轻巧以下是我展示台风动态路径展示 1.首先为大家展示一下动态台风所使用数据 上面中采用标准json格式数据,data数据中,points是对 ...
- Azure Kay Vault(一).NET Core Console App 获取密钥保管库中的机密信息
一,引言 Azure 密钥保管库用于存储敏感信息,例如链接字符串,密码,API 密钥等.我们无法直接从Azure 密钥库中访问机密!那么我们如何才能访问应用程序中的机密信息?比如,在我们的实际项目中, ...
- A*算法的有关知识--例子:最短路径问题
前置知识 定义1,g(n)=从树根到节点n的代价.当算法处理到某个节点时,g(n)是可以精确计算的. 定义2,h*(n)=从节点n到目标节点的优化路径的代价.一般不可知. 定义3,f*(n)=g(n) ...
- 如何学习C语言
总结学习 C 语言的几个步骤,其他编程语言基本类似. 看书 学习一门编程语言少不了先学习基本语法. C语言的语法也就是变量,数组.指针.表达式.逻辑操作.函数,宏定义等等.学习这些先买一本入门级书籍, ...
- 使用MQTT协议的4G DTU模块具有什么优势
什么是MQTT协议 要了解使用MQTT协议的4G DTU模块具有哪些优势,首先我们需要了解什么是MQTT协议,MQTT协议最早是IBM开发的一个即时通讯协议,它的主要是为大量计算能力有限且工作在低带宽 ...
- python实现非常有趣的数学问题
1.无重复数字的三位数 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? import itertools ret = [] for i in range(1, 5) ...
- python栈、队列、文件目录遍历
一. 栈与队列 关注公众号"轻松学编程"了解更多. 1. 栈 stack 特点:先进先出[可以抽象成竹筒中的豆子,先进去的后出来] 后来者居上 mystack = [] #压栈[向 ...