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容器创建的过程,好多定制化的东西都需要 ...
随机推荐
- RHEL8和CentOS8怎么重启网络
本文主要讲解如何重启RHEL 8或者CentOS 8网络以及如何解决RHEL8和CentOS8系统的网络管理服务报错,当我们安装好RHEL 8或者 CentOS 8,重启启动网络时,会出现以下报错 ...
- 【应用程序见解 Application Insights】在Application Insights中通过自定义查询结果定义指标并显示在Dashboard中
问题情形 通过Application Insights收集到指标数据后,如Request,Trace,Exception.但是默认的Insights图表不能满足业务的需求,需要自定义相应的类SQL语句 ...
- ps命令没有显示路径找到命令真实路径
top发现某程序占用大量资源,但ps查看看不到程序真实路径,查找真实路径. ps aux |grep COMMAND 找到PID ls /proc/ 里边有很多数字文件夹,找到PID相应的文件夹进去看 ...
- VScode如何配置c/c++运行环境
vscode如何配置c/c++环境 下载 Mingw 参考链接:https://blog.csdn.net/jiqiren_dasheng/article/details/103775488 笔者下载 ...
- Lambda表达式(一)
Lambda表达式其实就是实现SAM接口的语法糖,作用就是简化代码的冗余,同时可读性也好过匿名内部类. 以下先一步步演示是如何把大段的代码变成一句代码的,加强理解! 第一种正常的写法 1 public ...
- 关于天线长度及LC值的计算
一.天线长度与波长 1.天线最佳长度计算 理论和实践证明,当天线的长度为无线电信号波长的1/4时,天线的发射和接收转换效率最高.因此,天线的长度将根据所发射和接收信号的频率即波长来决定.只要知道对应发 ...
- Java数据结构-03单链表(二)
在之前我们封装了一些操作在接口类中,并在抽象类实现了相同的方法.下面我们开始写代码: 无头结点单链表:(注意下面的AbstractList是之前抽取的类,不是java.util包下的类) public ...
- 如何使用Python爬取基金数据,并可视化显示
本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理 以下文章来源于Will的大食堂,作者打饭大叔 前言 美国疫情越来越严峻,大选也进入 ...
- 爬虫双色球所有的历史数据并保存到SQLite
前言 上一篇介绍了双色球走势图是怎么实现的,这一篇介绍怎么实现爬虫所有的双色球历史数据,也可以同步分享怎么同步福彩3D数据.采用的C#来实现的. 同步双色球的地址:https://datachart. ...
- ZooKeeper CentOS7上安装
下载http://www.apache.org/dyn/closer.cgi/zookeeper(我下的是zookeeper-3.4.14) 1.创建 /usr/local/services/zook ...