前言

为什么要自定义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. Bigdecimal用法

    一.简介 Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算.双精度浮点型变量double可以处理16位有效数.在实际应用中,需要对更大或者更 ...

  2. 流畅的python-2

    一.python数据结构  ----------------> ()      []          {} List 列表[] 可变的 lst = [1,2,3,4] #改 lst[(元素下标 ...

  3. 深入刨析tomcat 之---第2篇,解决第3章bug 页面不显示内容http://localhost:8080/servlet/ModernServlet?userName=zhangyantao&password=1234

    writedby 张艳涛7月2日, 在学习第4张的过程中,发现了前一篇文章写的是关于1,2张的bug不用设置response响应头,需要在servlet的service()方法里面写是错误想 serv ...

  4. div 模拟alert弹出框

    --------------信息展示弹出框---------------- <style> .over{background-color: rgba(0, 0, 0, 0.7);displ ...

  5. Netty入门(一):ByteBuf

    网络数据的基本单位总是字节.Java NIO 提供了 ByteBuffer 作为它的字节容器,但是这个类使用起来过于复杂,而且也有些繁琐.Netty 的 ByteBuffer 替代品是 ByteBuf ...

  6. Optional解决空指针

    Java 8 Optional 类 Java 8 新特性 Optional 类是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象. O ...

  7. 升级到Spring 5.3.x之后,GC次数急剧增加,我TM人傻了

    最近我们项目升级到了 Spring Boot 2.4.6 + Spring Cloud 2020.0.x,通过我的另一系列即可看出:Spring Cloud 升级之路.但是升级后,我们发现 Young ...

  8. Mybatis-初见

    目录 介绍 示例 搭建环境 创建一个模块 CURD 万能Map 配置解析 环境配置 environments 属性 properties 类型别名 typeAliases 其他配置 映射器 mappe ...

  9. 腾讯云TDSQL监控库密码忘记问题解决实战

    首先,给大家介绍一下TDSQL.TDSQL MySQL 版(TDSQL for MySQL)是腾讯打造的一款分布式数据库产品,具备强一致高可用.全球部署架构.分布式水平扩展.高性能.企业级安全等特性, ...

  10. 指向结构的指针 struct结构名称 *结构指针变量名

    //指向结构的指针 struct结构名称 *结构指针变量名 //(*结构指针变量名).成员变量名//结构指针变量->成员变量名 1 #include<stdio.h> 2 #incl ...