1. 使用场景:在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载、初始化缓存、特定任务的注册、开启线程或程序来干某些事情等等。

2. 使用步骤:

  A. 监听类实现ApplicationListener接口 ;

  B. 将监听类添加到SpringApplication实例(两种方式:application.properties配置、java启动配置)。

3. 支持的事件类型:

  A. ApplicationStartedEvent —— spring boot启动开始时执行的事件;

  B. ApplicationEnvironmentPreparedEvent —— spring boot 对应Enviroment已经准备完毕,但此时上下文context还没有创建;

  C. ApplicationPreparedEvent —— spring boot上下文context创建完成,但此时spring中的bean是没有完全加载完成的;

  D. ApplicationFailedEvent —— spring boot启动异常时执行事件 。

4.Spring内置的事件:

  A.  ContextRefreshedEvent —— ApplicationContext容器初始化或者刷新时触发该事件;

  B.  ContextStartedEvent —— 当使用ConfigurableApplicationContext接口的start()方法启动ApplicationContext容器时触发该事件;

  C. ContextClosedEvent —— 当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext容器时触发该事件;

  D. ContextStopedEvent —— 当使用ConfigurableApplicationContext接口的stop()方法停止ApplicationContext容器时触发该事件。

5.举例说明:

package com.ruhuanxingyun;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.nsac.business.listener.ApplicationStartup; @SpringBootApplication
public class SpApp {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(SpApp.class);
springApplication.addListeners(new ApplicationStartup());
springApplication.run(args);
}
}
package com.nsac.ruhuanxingyun;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent; public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
ApplicationContext ac = event.getApplicationContext();
StepExecutor StepExecutor = new StepExecutor(ac.getEnvironment().getProperty("project-id"), ac.getBean("businessSingleJedisPool",redis.clients.jedis.JedisPool.class),
                                ac.getBean("redisCluster", redis.clients.jedis.JedisCluster.class));
Thread thread = new Thread(StepExecutor);
thread.start();
} }

可参考:https://blog.csdn.net/liaokailin/article/details/48186331

SpringBoot 自定义监听器(Listener)的更多相关文章

  1. SpringBoot新增监听器Listener

    什么是web监听器? web监听器是一种Servlet中的特殊的类,它们能帮助开发者监听web中的特定事件,比如ServletContext,HttpSession,ServletRequest的创建 ...

  2. 【java web】监听器listener

    一.简介 Java的监听器,也是系统级别的监听.监听器随web应用的启动而启动.Java的监听器在c/s模式里面经常用到,它会对特定的事件产生产生一个处理.监听在很多模式下用到,比如说观察者模式,就是 ...

  3. SpringBoot(10) Servlet3.0的注解:自定义原生Servlet、自定义原生Listener

    一.自定义原生Servlet 1.启动类里面增加注解 @ServletComponentScan 2.Servlet上添加注解  @WebServlet(name = "userServle ...

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

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

  5. SpringBoot自定义servlet、注册自定义的servlet、过滤器、监听器、拦截器、切面、webmvcconfigureradapter过时问题

    [转]https://www.cnblogs.com/NeverCtrl-C/p/8191920.html 1 servlet简介 servlet是一种用于开发动态web资源的技术 参考博客:serv ...

  6. javaweb学习总结(四十七)——监听器(Listener)在开发中的应用

    监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...

  7. javaWeb学习总结(11)- 监听器(Listener)在开发中的应用

    监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...

  8. [09] 监听器 Listener

    1.事件 1.1 事件的概念 在Servlet中有一个概念叫做监听,顾名思义,就是监听某种事件是否发生.就如你是一家娱乐媒体公司的老板,你派出狗仔队去跟着某些明星,比如你想了解他们的绯闻,或者活动进展 ...

  9. 过滤器(Filter)、拦截器(Interceptor)、监听器(Listener)

    一.Filter 过滤器 1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servle ...

随机推荐

  1. 父元素高度设置为min-height,子元素高度设置为100%,但实际上子元素高度你知道是多少吗?

    前言 给父元素一个min-height,子元素设置height:100%. 代码 <!DOCTYPE html> <html> <head> <title&g ...

  2. 6-4 The present perfect

    1 Summary The present perfect is an important verb tense in English. It is used to talk about things ...

  3. golang介绍

    一.golang介绍  golang是Google开发的一种 静态强类型.编译型,并发型,并具有垃圾回收功能的编程语言. 二.语言特性 1..自动垃圾回收 2.支持函数多返回值 3.并发强 三.gol ...

  4. 15.service认证机制

    kubernetes的安全机制,不仅仅再客户端和apisever之间的交互,各pod之间后者跨namespace之间也要以此为基准配合 RBAC来进行.提到另一个账户关系serviceaccount简 ...

  5. bis和bic命令实现或和异或运算

    从20世纪70年代末到80年代末,Digital Equipment的VAX计算机是一种非常流行的机型.它没有布尔运算AND和OR指令,只有bis(位设置)和bic(位清除)这两种指令.两种指令的输入 ...

  6. Magento 2 Plugin - Interceptor - Magento 2插件 - 拦截器-插件开发

    Magento 2 Plugin - Interceptor - Magento 2插件 - 拦截器 Magento 2 Plugin is a technical plugin for your b ...

  7. LOJ #2135. 「ZJOI2015」幻想乡战略游戏(点分树)

    题意 给你一颗 \(n\) 个点的树,每个点的度数不超过 \(20\) ,有 \(q\) 次修改点权的操作. 需要动态维护带权重心,也就是找到一个点 \(v\) 使得 \(\displaystyle ...

  8. Node.js修改全局安装默认路径

    因为苦于C盘不够的烦恼,不想把全局安装包的路径弄在C盘,于是有了这篇文章: 查看设置 npm config ls //查看设定信息,,找到prefix一行,默认是一般是在C盘 修改命令如下 npm c ...

  9. badboy安装及使用

    badboy下载 下载地址:http://www.badboy.com.au/download/index 直接点击[continue] badboy安装 badboy录制 默认是录制状态 访问sog ...

  10. A.01.12—模块的输出—通讯(CAN&LIN)

    AN和LIN相关的内容很多,今天仅对几年前困扰过我的一个疑问进行说明. 以前最常见的通迅方式为CAN和LIN,但现在也有很多其他的通讯方式了,而这两种通讯方式仍使用广泛. 前几年常听人说CAN的成本和 ...