自定义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 ...
随机推荐
- P4169-CDQ分治/K-D tree(三维偏序)-天使玩偶
P4169-CDQ分治/K-D tree(三维偏序)-天使玩偶 这是一篇两种做法都有的题解 题外话 我写吐了-- 本着不看题解的原则,没写(不会)K-D tree,就写了个cdq分治的做法.下面是我的 ...
- JavaScript之DOM、DOM树
一 DOM JavaScript操作网页的接口,全称为"文档对象模型"(Document Object Model). 有这几个概念:文档.元素.节点 整个文档是一个文档节点 每个 ...
- c++ 父类 子类 虚表占用内存空间情况
#include <iostream> using namespace std; class C {}; class A:public C { private: long a; long ...
- input输入框只能输入正数和小数(保留小数点后两位)
1.限制只能输入正数和小数保留小数点后两位 1 <input type="number" id="txtNum" /> 2 3 <script ...
- ABC133简要题解
A T or T TOT 模拟即可 B Good Distance \(O(n^2)\) 模拟. C Remainder Minimization 2019 把 \(r\) 变成 \(l+2019\) ...
- 双非本科Android开发,如何逆袭拿到大厂 Offer?
从2020年3月18日投出第一份暑期实习简历至今,已经过去400多天.我也尘埃落定,即将去CVTE做Android开发. 休息了很长时间,如今已经能够很平静地回首这段历程,写下这篇文,致敬曾经走过的漫 ...
- matplotlib.pyplot设置画布主题
import matplotlib.pyplot as plt # 定义一个画图函数 def sinplot(flip = 1): x = np.linspace(0,10,100) for i in ...
- 能够进行多段文本匹配的NFA改良算法
下面的代码基于NFA算法实现了在多段字符串中匹配正则表达式,对比NFA算法可以看到它将pc由局部变量提升为类成员,以保存中间匹配状态,另外在匹配成功后将pc恢复到null状态.实际使用中,此类还应该增 ...
- C++小知识——显示VS大括号/花括号折叠按钮
这个功能默认是关闭的,打开路径如下: 将大纲语句块改为"True" 这个功能其实很有必要真不知道为啥默认要关闭这个功能. 站在巨人的肩膀上的思想,其实已经在互联网程序员之间深入人心 ...
- 【Java】jeesite初始配置以及代码生成工具的使用
jeesite简单使用 首先去技术服务与支持.版本区别一览表 - JeeSite 4.x找到源码下载的部分 JeeSite 源码下载:https://gitee.com/thinkgem/jeesit ...