博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot实战(第二篇)事件监听
阅读量:6987 次
发布时间:2019-06-27

本文共 5404 字,大约阅读时间需要 18 分钟。

http://blog.csdn.net/liaokailin/article/details/48186331

前言

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
{ private Logger logger = LoggerFactory.getLogger(MyApplicationStartedEventListener.class); @Override public 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 implements ApplicationListener
{ private Logger logger = LoggerFactory.getLogger(MyApplicationEnvironmentPreparedEventListener.class); @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment envi = event.getEnvironment(); MutablePropertySources mps = envi.getPropertySources(); if (mps != null) { Iterator
> 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
{ private Logger logger = LoggerFactory.getLogger(MyApplicationPreparedEventListener.class); @Override public 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
{ @Override public 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() { @Override public void run() { doClose(); } }; Runtime.getRuntime().addShutdownHook(this.shutdownHook); } }

 

doClose()方法中进行资源的回收与释放。

结束语

spring boot提供的四种监听事件到这里就结束了,针对实际业务可添加自定义的监听器,下一节当中将会对spring boot中的监听源码进行分析,理解为什么是这样的。

你可能感兴趣的文章
关于磁盘,磁柱,磁头,扇区的概念
查看>>
Layer 父子页面之间的交互
查看>>
WPF
查看>>
IQ推理:红眼睛和蓝眼睛
查看>>
Python中lambda使用简易教程
查看>>
函数调用运算符"()"
查看>>
PHP数组传递给JavaScript以及json_encode的gbk中文乱码的解决
查看>>
<转>python version 2.7 required,which was not found in the registry
查看>>
Mysql捕捉(网站)应用执行的语句
查看>>
Sicily/1282. Computer Game
查看>>
语言那点事,crt
查看>>
获取当前路径
查看>>
剑指offer66题 -- 输入一个链表,从尾到头打印链表每个节点的值
查看>>
图解算法(五)
查看>>
popupwindow
查看>>
甲骨文中国公司如何如何打赢这场裁员翻身仗?
查看>>
visio 转 pdf 裁边 ——使用pdfcrop对PDF裁边
查看>>
空指针异常
查看>>
线程队列,线程池和协程
查看>>
第3章 理解XP
查看>>