前言

为什么要自定义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. GC相关问题

    为什么会有新生代? 如果不分代,所有对象全部在一个区域,每次GC都需要对全堆进行扫描,存在效率问题.分代后,可分别控制回收频率,并采用不同的回收算法,确保GC性能全局最优. 为什么新生代会采用复制算法 ...

  2. Apache ActiveMQ(cve-2015-5254)

    影响版本 Apache ActiveMQ 5.13.0之前5.x版本中存在安全漏洞 复现 使用工具执行命令 工具地址 https://github.com/matthiaskaiser/jmet/re ...

  3. centos 服务

    1,开机开启服务 #  chkconfig vsftpd on

  4. 《Android原生整合虹软SDK开发uniapp插件》

    1.项目背景 应公司要求,需要开发一套类似人脸打卡功能的app,但是因为我们公司没有很强的原生android开发者,所以根据现状选择了第三方跨平台的uniapp,想必目前大多人都了解这个平台了,我也就 ...

  5. 第4篇-JVM终于开始调用Java主类的main()方法啦

    在前一篇 第3篇-CallStub新栈帧的创建 中我们介绍了generate_call_stub()函数的部分实现,完成了向CallStub栈帧中压入参数的操作,此时的状态如下图所示. 继续看gene ...

  6. JavaScript学习07(jQuery)

    jQuery 什么是JQuery jQuery 由 John Resig 于 2006 年创建.它旨在处理浏览器不兼容性并简化 HTML DOM 操作.事件处理.动画和 Ajax. 十多年来,jQue ...

  7. Git-02-版本回退

    环境准备 1 修改readme.txt内容 Git is a distributed version control system. Git is free software. 2 git statu ...

  8. Python语言系列-04-高阶函数

    闭包 #!/usr/bin/env python3 # author:Alnk(李成果) # 什么是闭包 # 1,闭包存在于函数中 # 2,闭包就是内层函数对外层函数(非全局变量)的引用 # 3,最内 ...

  9. SQL注入的那些面试题总结

    一.知识储备类 1.SQL与NoSQL的区别? SQL:关系型数据库 NoSQL:非关系型数据库 存储方式:SQL具有特定的结构表,NoSQL存储方式灵活 性能:NoSQL较优于SQL 数据类型:SQ ...

  10. Fiddler抓包实用非常详细,学会不要去做坏事~

    为什么要先学fiddler?学习接口测试必学http协议,如果直接先讲协议,我估计小伙伴们更懵,为了更好的理解协议,先从抓包开始.结合抓包工具讲http协议更容易学一些. 抓firefox上https ...