前言

为什么要自定义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的更多相关文章

  1. Spring Boot 启动过程及 自定义 Listener等组件

    一.启动过程 二.自定义组件 package com.example.jdbc.listener; import org.springframework.context.ApplicationCont ...

  2. 如何在自定义Listener(监听器)中使用Spring容器管理的bean

    正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...

  3. web.xml中自定义Listener

    Listener可以监听容器中某一执行动作,并根据其要求做出相应的响应. 常用的Web事件的监听接口如下: ServletContextListener:用于监听Web的启动及关闭 ServletCo ...

  4. 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener

    =================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...

  5. Servlet3.0的注解自定义原生Listener监听器实战

    简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战 自定义Listener(常用的监听器 servletContextListener.httpSessionListen ...

  6. SpringBoot中使用Servlet,Filter,Listener

    项目最近在替换之前陈旧的框架,改用SpringBoot进行重构,初接触,暂时还没有用到Servlet,Filter,Listener的地方,但在之前回顾Servlet的生命周期时,https://ww ...

  7. JUnit源码分析 - 扩展 - 自定义RunListener

    RunListener简述 JUnit4中的RunListener类用来监听测试执行的各个阶段,由RunNotifier通知测试去运行.RunListener与RunNotifier之间的协作应用的是 ...

  8. 【Java Web开发学习】Spring MVC添加自定义Servlet、Filter、Listener

    [Java Web开发学习]Spring MVC添加自定义Servlet.Filter.Listener 转载:https://www.cnblogs.com/yangchongxing/p/9968 ...

  9. SpringBoot系列教程web篇Listener四种注册姿势

    java web三要素Filter, Servlet前面分别进行了介绍,接下来我们看一下Listener的相关知识点,本篇博文主要内容为SpringBoot环境下,如何自定义Listener并注册到s ...

随机推荐

  1. Tarjan判断为什么不能把dfn写成low

    Tarjan,我相信大多数人是这么写的: void tarjan(int x) { dfn[x]=low[x]=++cnt; st.push(x),vis[x]=1; for(int i=head[x ...

  2. [考试总结]noip18

    发现之前咕掉了这个考试的总结. 今天就把它给补上. 这也是一个炸裂的一场 开局以为 \(T1\) 可做,然而事实证明我又错了... 莽了一个随机化上去,轻松过了所有样例... 以为稳了 然而挂掉了.. ...

  3. odoo源码学习之任务中的阶段字段stage_id

    # 案例0004针对form表单 class Task(models.Model): _name = "project.task" _description = "对于项 ...

  4. python中进程详解

    1:pdb调试:基于命令行的调试工具,非常类似gnu和gdb调试,以下是常用的调试命令: 可以python -m pdb xxx.py(你的py文件名)进入命令行调试模式 命令 简写命令 作用 bea ...

  5. n皇后问题(回溯法)——Python实现

      八皇后问题 问题: 国际象棋棋盘是8 * 8的方格,每个方格里放一个棋子.皇后这种棋子可以攻击同一行或者同一列或者斜线(左上左下右上右下四个方向)上的棋子.在一个棋盘上如果要放八个皇后,使得她们互 ...

  6. oracle Group by 分组查询后,分页

    ------------恢复内容开始------------ 1.分页查询 select count(*) times,title from menulog group by title order ...

  7. Java 中节省 90% 时间的常用的工具类

    前言 你们有木有喜欢看代码的领导啊,我的领导就喜欢看我写的代码,有事没事就喜欢跟我探讨怎么写才最好,哈哈哈...挺好. 今天我们就一起来看看可以节省 90% 的加班时间的第三方开源库吧,第一个介绍的必 ...

  8. SpringCloud-Alibaba 最新的 依赖版本管理组合以及 整合gateway遇到的问题

    一般来说,cloud 与 alibaba 拥有版本组合说明文档wiki,不过这里可以做一个最新的组合的分享. <dependencyManagement> <dependencies ...

  9. 一张图带你搞懂Javascript原型链关系

    在某天,我听了一个老师的公开课,一张图搞懂了原型链. 老师花两天时间理解.整理的,他讲了两个小时我们当时就听懂了. 今天我把他整理出来,分享给大家.也让我自己巩固加深一下. 就是这张图: 为了更好的图 ...

  10. Spring Cloud Gateway自定义异常处理Exception Handler

    版本: Spring Cloud 2020.0.3 常见的方法有 实现自己的 DefaultErrorWebExceptionHandler 或 仅实现ErrorAttributes. 方法1: Er ...