SpringBoot 自定义监听器(Listener)
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)的更多相关文章
- SpringBoot新增监听器Listener
什么是web监听器? web监听器是一种Servlet中的特殊的类,它们能帮助开发者监听web中的特定事件,比如ServletContext,HttpSession,ServletRequest的创建 ...
- 【java web】监听器listener
一.简介 Java的监听器,也是系统级别的监听.监听器随web应用的启动而启动.Java的监听器在c/s模式里面经常用到,它会对特定的事件产生产生一个处理.监听在很多模式下用到,比如说观察者模式,就是 ...
- SpringBoot(10) Servlet3.0的注解:自定义原生Servlet、自定义原生Listener
一.自定义原生Servlet 1.启动类里面增加注解 @ServletComponentScan 2.Servlet上添加注解 @WebServlet(name = "userServle ...
- Servlet3.0的注解自定义原生Listener监听器实战
简介:监听器介绍和Servlet3.0的注解自定义原生Listener监听器实战 自定义Listener(常用的监听器 servletContextListener.httpSessionListen ...
- SpringBoot自定义servlet、注册自定义的servlet、过滤器、监听器、拦截器、切面、webmvcconfigureradapter过时问题
[转]https://www.cnblogs.com/NeverCtrl-C/p/8191920.html 1 servlet简介 servlet是一种用于开发动态web资源的技术 参考博客:serv ...
- javaweb学习总结(四十七)——监听器(Listener)在开发中的应用
监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...
- javaWeb学习总结(11)- 监听器(Listener)在开发中的应用
监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用 一.统计当前在线人数 在JavaWeb应用开发中,有时候我们需要统计当前在线的用户数,此时就可以使用监听 ...
- [09] 监听器 Listener
1.事件 1.1 事件的概念 在Servlet中有一个概念叫做监听,顾名思义,就是监听某种事件是否发生.就如你是一家娱乐媒体公司的老板,你派出狗仔队去跟着某些明星,比如你想了解他们的绯闻,或者活动进展 ...
- 过滤器(Filter)、拦截器(Interceptor)、监听器(Listener)
一.Filter 过滤器 1.简介 Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servle ...
随机推荐
- linux下安装PHP扩展memcache
公司的服务器 CentOS 7.5,PHP 5.6 下面都是最新的版本(支持到PHP5.6) 如需php7 下支持memcache扩展,请移步 https://www.cnblogs.com/h ...
- Shell命令-文件及目录操作之cp、find
文件及目录操作 - cp.find 1.cp:复制文件或目录 cp命令的功能说明 cp命令用于复制文件或目录. cp命令的语法格式 cp [OPTION]... SOURCE... DIRECTORY ...
- elastalert 配置post告警方式(备忘)
最近在做把elk告警日志发送到kinesis 流,供后续数据分析处理使用........ 基于尽量不修改elastalert ,把修改工作放到接收端服务的原则.计划把elk的告警数据通过远程api ...
- 【Sql Server】SQL SERVER 递归查询
SQL SERVER 2005之前的版本只能用函数方法实现,SQL SERVER 2005之后新增了CTE功能,可以利用CTE实现递归查询: CTE:公用表达式Common Table Express ...
- 2018-2019-2 《Java程序设计》第3周学习总结
20175319 2018-2019-2 <Java程序设计>第3周学习总结 教材学习内容总结 第三周通过课本与蓝墨云上的视频学习了<Java2实用教程>第四章类与对象 成功激 ...
- Spring Cloud使用样例
Spring Cloud Demo 项目地址:https://github.com/hackyoMa/spring-cloud-demo 组件 基于Spring Boot 2.0.4.Spring C ...
- Correction suggestions
1. title: A remark on the global existence of weak solutions to the compressible quantum Navier-Stok ...
- [物理学与PDEs]第2章习题13 将 $p$ - 方程组化为守恒律形式的一阶拟线性对称双曲组
试引进新的未知函数, 将 $p$ - 方程组 $$\beex \bea \cfrac{\p \tau}{\p t}-\cfrac{\p u}{\p x}&=0,\\ \cfrac{\p u}{ ...
- A Basic Course in Partial Differential Equations
A Basic Course in Partial Differential Equations, Qing Han, 2011 [下载说明:点击链接,等待5秒, 点击右上角的跳过广告后调至下载页面, ...
- fetch 的控制器和观察者
因为受 cancelable promise 的拖延,fetch 一直没有传统的 XHR 所拥有的 abort() 和 onprogress 功能,去年年底 cancelable promise 草案 ...