自定义Listener
前言
为什么要自定义Listener?
通过自定义,可以在程序启动过程中监听特定事件,再回调处理逻辑。
自定义SpringApplicationRunListener
1、创建自定义SpringApplicationRunListener,在回调方法中实现自定义的处理逻辑
public class CustomSpringRunListener implements SpringApplicationRunListener {
public CustomSpringRunListener(SpringApplication application, String[] args) {
System.out.println("CustomSpringRunListener constructor");
}
public void starting() {
}
public void environmentPrepared(ConfigurableEnvironment environment) {
}
public void contextPrepared(ConfigurableApplicationContext context) {
}
public void contextLoaded(ConfigurableApplicationContext context) {
}
public void started(ConfigurableApplicationContext context) {
}
public void running(ConfigurableApplicationContext context) {
}
public void failed(ConfigurableApplicationContext context, Throwable exception) {
}
}
2、在META/spring.factories中声明自定义Listener
org.springframework.boot.SpringApplicationRunListener=\
edu.xjh.test.listener.CustomSpringRunListener
从而,SpringBoot启动过程中会扫描META/spring.factories,加载所有的SpringApplicationRunListener
自定义ApplicationListener
1、自定义ApplicationListener,实现onApplicationEvent逻辑
public class CustomApplicationListener implements ApplicationListener<ApplicationEvent> {
public void onApplicationEvent(ApplicationEvent event) {
// 判断事件类型,执行特定处理逻辑
if (event instanceof ApplicationStartingEvent) {
System.out.println("CustomApplicationListener catch ApplicationStartingEvent");
}else if (event instanceof ApplicationEnvironmentPreparedEvent) {
System.out.println("CustomApplicationListener catch ApplicationEnvironmentPreparedEvent");
}
}
}
2、声明ApplicationListener
方法1:
在META/spring.factories中声明自定义ApplicationListener,因为启动过程中会扫描该文件进行加载
org.springframework.context.ApplicationListener=\
edu.xjh.test.listener.CustomApplicationListener
方法2:
在实例化SpringApplication之后,手动添加Listener
public static void main(String[] args) {
SpringApplication application = new SpringApplication(DemoApp.class);
application.addListeners(new CustomApplicationListener());
application.run(args);
}
自定义Listener的更多相关文章
- Spring Boot 启动过程及 自定义 Listener等组件
一.启动过程 二.自定义组件 package com.example.jdbc.listener; import org.springframework.context.ApplicationCont ...
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...
- web.xml中自定义Listener
Listener可以监听容器中某一执行动作,并根据其要求做出相应的响应. 常用的Web事件的监听接口如下: ServletContextListener:用于监听Web的启动及关闭 ServletCo ...
- 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener
=================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...
- Servlet3.0的注解自定义原生Listener监听器实战
简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战 自定义Listener(常用的监听器 servletContextListener.httpSessionListen ...
- SpringBoot中使用Servlet,Filter,Listener
项目最近在替换之前陈旧的框架,改用SpringBoot进行重构,初接触,暂时还没有用到Servlet,Filter,Listener的地方,但在之前回顾Servlet的生命周期时,https://ww ...
- JUnit源码分析 - 扩展 - 自定义RunListener
RunListener简述 JUnit4中的RunListener类用来监听测试执行的各个阶段,由RunNotifier通知测试去运行.RunListener与RunNotifier之间的协作应用的是 ...
- 【Java Web开发学习】Spring MVC添加自定义Servlet、Filter、Listener
[Java Web开发学习]Spring MVC添加自定义Servlet.Filter.Listener 转载:https://www.cnblogs.com/yangchongxing/p/9968 ...
- SpringBoot系列教程web篇Listener四种注册姿势
java web三要素Filter, Servlet前面分别进行了介绍,接下来我们看一下Listener的相关知识点,本篇博文主要内容为SpringBoot环境下,如何自定义Listener并注册到s ...
随机推荐
- Python自动化测试面试题-Linux篇
目录 Python自动化测试面试题-经验篇 Python自动化测试面试题-用例设计篇 Python自动化测试面试题-Linux篇 Python自动化测试面试题-MySQL篇 Python自动化测试面试 ...
- 如何选择数据分析工具?BI工具需要具备哪些功能?
数据分析使企业能够分析其所有数据(实时,历史,非结构化,结构化,定性),以识别模式并生成洞察力,以告知并在某些情况下使决策自动化,将数据情报与行动联系起来.当今最好的数据分析工具解决方案支持从访问.准 ...
- DNS的原理和解析过程
DNS的解析原理和过程: 在Internet上域名和IP是对应的,DNS解析有两种:一种是正向解析,另外一种是反向解析. 正向解析:正向解析就是将域名转换成对应的 IP地址的过程,它应用于在浏览器地址 ...
- JS对DOM的操作优化法则
html页面显示过程 解析HTML,并生成一棵DOM tree 解析各种样式并结合DOM tree生成一棵Render tree 对Render tree的各个节点计算布局信息,比如box的位置与尺寸 ...
- [.NET大牛之路 005] .NET 的执行模型
.NET大牛之路 • 王亮@精致码农 • 2021.07.06 前面我们介绍 .NET 历史时讲过,微软基于 .NET Framework 重新设计并创造了跨平台的 .NET Core,目前已经发展到 ...
- Apache httpd的web服务
Apache httpd的web服务 适用于Unix/Linux下的web服务器软件 Apache httpd(开源且免费),虚拟主机,支持HTTPS协议,支持用户认证,支持单个目录的访问控制,支持U ...
- Clickhouse Docker集群部署
写在前面 抽空来更新一下大数据的玩意儿了,起初架构选型的时候有考虑Hadoop那一套做数仓,但是Hadoop要求的服务器数量有点高,集群至少6台或以上,所以选择了Clickhouse(后面简称CH). ...
- CMMI相关图书
Integrating CMMI and Agile Development: Case Studies and Proven Techniques for Faster Performance Im ...
- VBA·Function的基础使用
阅文时长 | 0.27分钟 字数统计 | 440字符 主要内容 | 1.引言&背景 2.基本结构 3.Demo示例 4.声明与参考资料 『VBA·Function的基础使用』 编写人 | SC ...
- hdmi 随笔
从图片来看,每张图片开始传输的是45像素的垂直同步, 1.控制数据贯穿所有时间,没个不是控制数据的传输都被控制数据包围.控制数据还要通过控制位指示,下一个数据是数据岛还是视频信号. 2.terc4 全 ...