spring boot实战(第二篇)事件监听
前言
spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利。
支持的事件类型四种
ApplicationStartedEvent
ApplicationEnvironmentPreparedEvent
ApplicationPreparedEvent
ApplicationFailedEvent
实现监听步骤:
1.监听类实现ApplicationListener
接口
2.将监听类添加到SpringApplication
实例
ApplicationStartedEvent
ApplicationStartedEvent
:spring boot启动开始时执行的事件
创建对应的监听类 MyApplicationStartedEventListener.java
package com.lkl.springboot.listener;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;/*** spring boot 启动监听类* * @author liaokailin* @version $Id: MyApplicationStartedEventListener.java, v 0.1 2015年9月2日 下午11:06:04 liaokailin Exp $*/
public class MyApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {private Logger logger = LoggerFactory.getLogger(MyApplicationStartedEventListener.class);@Overridepublic void onApplicationEvent(ApplicationStartedEvent event) {SpringApplication app = event.getSpringApplication();app.setShowBanner(false);// 不显示banner信息logger.info("==MyApplicationStartedEventListener==");}
}
在该事件中可以获取到SpringApplication
对象,可做一些执行前的设置.
Application.java
类
package com.lkl.springboot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import com.lkl.springboot.listener.MyApplicationStartedEventListener;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication app = new SpringApplication(Application.class); app.addListeners(new MyApplicationStartedEventListener());app.run(args);}
}
ApplicationEnvironmentPreparedEvent
ApplicationEnvironmentPreparedEvent
:spring boot 对应Enviroment已经准备完毕,但此时上下文context还没有创建。
MyApplicationEnvironmentPreparedEventListener.java
package com.lkl.springboot.listener;import java.util.Iterator;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;/*** spring boot 配置环境事件监听* @author liaokailin* @version $Id: MyApplicationEnvironmentPreparedEventListener.java, v 0.1 2015年9月2日 下午11:21:15 liaokailin Exp $*/
public class MyApplicationEnvironmentPreparedEventListener implementsApplicationListener<ApplicationEnvironmentPreparedEvent> {private Logger logger = LoggerFactory.getLogger(MyApplicationEnvironmentPreparedEventListener.class);@Overridepublic void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {ConfigurableEnvironment envi = event.getEnvironment();MutablePropertySources mps = envi.getPropertySources();if (mps != null) {Iterator<PropertySource<?>> iter = mps.iterator();while (iter.hasNext()) {PropertySource<?> ps = iter.next();logger.info("ps.getName:{};ps.getSource:{};ps.getClass:{}", ps.getName(), ps.getSource(), ps.getClass());}}}}
在该监听中获取到ConfigurableEnvironment
后可以对配置信息做操作,例如:修改默认的配置信息,增加额外的配置信息等等~~~
ApplicationPreparedEvent
ApplicationPreparedEvent
:spring boot上下文context创建完成,但此时spring中的bean是没有完全加载完成的。
MyApplicationPreparedEventListener.java
package com.lkl.springboot.listener;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;/*** 上下文创建完成后执行的事件监听器* * @author liaokailin* @version $Id: MyApplicationPreparedEventListener.java, v 0.1 2015年9月2日 下午11:29:38 liaokailin Exp $*/
public class MyApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {private Logger logger = LoggerFactory.getLogger(MyApplicationPreparedEventListener.class);@Overridepublic void onApplicationEvent(ApplicationPreparedEvent event) {ConfigurableApplicationContext cac = event.getApplicationContext();passContextInfo(cac);}/*** 传递上下文* @param cac*/private void passContextInfo(ApplicationContext cac) {//dosomething()}}
在获取完上下文后,可以将上下文传递出去做一些额外的操作。
在该监听器中是无法获取自定义bean并进行操作的。
ApplicationFailedEvent
ApplicationFailedEvent
:spring boot启动异常时执行事件
MyApplicationFailedEventListener.java
package com.lkl.springboot.listener;import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.ApplicationListener;public class MyApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {@Overridepublic void onApplicationEvent(ApplicationFailedEvent event) {Throwable throwable = event.getException();handleThrowable(throwable);}/*处理异常*/private void handleThrowable(Throwable throwable) {}}
在异常发生时,最好是添加虚拟机对应的钩子进行资源的回收与释放,能友善的处理异常信息。
在spring boot中已经为大家考虑了这一点,默认情况开启了对应的功能:
public void registerShutdownHook() {if (this.shutdownHook == null) {// No shutdown hook registered yet.this.shutdownHook = new Thread() {@Overridepublic void run() {doClose();}};Runtime.getRuntime().addShutdownHook(this.shutdownHook);}}
在doClose()
方法中进行资源的回收与释放。
spring boot实战(第二篇)事件监听相关推荐
- Spring Boot————ApplicationListener实现逃课事件监听
引言 上一篇文章转了一篇关于ApplicationListener用于在Web项目启动时做一些初始化的用法. 但是,在实际生产过程中,当一个事件产生,又是如何被onApplicationEvent() ...
- 【COCOS CREATOR 系列教程之二】脚本开发篇事件监听、常用函数等示例整合
本站文章均为 李华明Himi 原创,转载务必在明显处注明: 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/cocos-creator/1959.htm ...
- html中事件监听的使用,Vue.JS入门篇--事件监听
你可以使用 v-on 指令来绑定并监听 DOM 事件.绑定的内容可以是一个当前实例上的方法 (后面无需跟括号) 或一个内联表达式.如果提供的是一个方法,则原生的 DOM event 会被作为第一个参数 ...
- spring boot实战(第九篇)Application创建源码分析
前言 通过前面的文章了解到在spring boot的启动时,利用的是编写的Application类,使用了注解@SpringBootApplication,本篇将阐述该Bean的加载过程. [html ...
- Spring Boot————应用启动时的监听机制测试
引言 本文承接前面的<Spring Boot----Spring Boot启动流程分析>,主要测试一下ApplicationContextInitializer.SpringApplica ...
- spring boot实战(第六篇)加载application资源文件源码分析
前言 在上一篇中了解了spring配置资源的加载过程,本篇在此基础上学习spring boot如何默认加载application.xml等文件信息的. ConfigFileApplicationLis ...
- Spring5源码 - 11 Spring事件监听机制_源码篇
文章目录 pre 事件监听机制的实现原理[观察者模式] 事件 ApplicationEvent 事件监听者 ApplicationEvent 事件发布者 ApplicationEventMultica ...
- java 事件监听应用_Spring Boot应用事件监听示例详解
前言 本文主要给大家介绍了关于Spring Boot应用事件监听的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 1. Spring Boot特有的应用事件 除了Spring ...
- Spring事件监听流程分析【源码浅析】
一.简介 Spring早期是通过实现ApplicationListener接口来定义监听事件,在spring4.2的时候开始我们可以通过@EventListener注解来定义监听事件,Applicat ...
最新文章
- qchart画完以后删除_画错了,重新画一幅吧!”这句话对学画画的孩子来说,伤害有多大?...
- 祝福我的朋友们五一玩的开心!
- Zencart修改前台页面的字体颜色
- 【html、css、jq】制作一个简洁的音乐播放器
- Linux执行yum不显示时间图形,Linux停的yum命令详解(朝花夕拾)
- excel取消隐藏_Excel技巧:批量删除隐藏数据及隐藏工作表
- 如何使用json开发web_如何通过使用JSON Web令牌简化应用程序的身份验证
- html缓存失败是什么,HTML5离线“应用程序缓存错误事件:清单读取失败(-1)”...
- 在CSDN发布付费资源
- 你专属的程序员春节“大礼包”
- Delcam PowerInspect 5040 sp1/
- 刚刚,百度宣布造车!
- java 使用poi将PPT转为图片,在线预览PPT
- mySQL:Access denied for user 'root'@'127.0.0.1' to database 'information_schema'
- 高考数学有得用计算机吗,高中数学为什么不让用计算器?
- MySQL权限与安全管理之权限表与账户管理
- HTTPSConnectionPool(host=‘finance.yahoo.com‘, port=443解决方案
- OpenCV -5 -GoogLeNet模型使用及CNN理论
- Vulkan spec 中文版 翻译基础版本切换
- 转自何海涛 编程面试的五个要点