SpringBoot入门之事件监听
spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利,sptingboot支持的事件类型有以下五种:
- ApplicationStartingEvent
- ApplicationFailedEvent
- ApplicationPreparedEvent
- ApplicationReadyEvent
- ApplicationEnvironmentPreparedEvent
实现监听步骤
1.监听类实现ApplicationListener接口
2.将监听类添加到SpringApplication实例中
ApplicationStartingEvent
ApplicationStartingEvent:springboot启动开始的时候执行的事件,在该事件中可以获取到SpringApplication对象,可做一些执行前的设置。
package com.ysl.listener; import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener; /**
* springboot启动监听类
*/
public class MyApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent>{ @Override
public void onApplicationEvent(ApplicationStartingEvent applicationStartingEvent) {
SpringApplication application = applicationStartingEvent.getSpringApplication();
application.setBannerMode(Banner.Mode.OFF);
System.out.println("--------- execute MyApplicationStartingEventListener----------");
} }
package com.ysl; import com.ysl.listener.MyApplicationStartingEventListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class Application { public static void main(String[] args){
SpringApplication app =new SpringApplication(Application.class);
app.addListeners(new MyApplicationStartingEventListener());
app.run(args);
}
}
执行结果如下:
--------- execute MyApplicationStartingEventListener----------
-- ::58.027 INFO --- [ main] com.ysl.Application : Starting Application on master with PID (/home/workspace/springboottest/target/classes started by ysl in /home/workspace/springboottest)
-- ::58.032 INFO --- [ main] com.ysl.Application : No active profile set, falling back to default profiles: default
-- ::58.182 INFO --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f32bf7: startup date [Sat Mar :: CST ]; root of context hierarchy
ApplicationEnvironmentPreparedEvent
ApplicationEnvironmentPreparedEvent:spring boot 对应Enviroment已经准备完毕,但此时上下文context还没有创建。在该监听中获取到ConfigurableEnvironment后可以对配置信息做操作,例如:修改默认的配置信息,增加额外的配置信息等等。
package com.ysl.listener; 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; import java.util.Iterator; /**
* spring boot 配置环境事件监听
*/
public class MyApplicationEnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent>{ @Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
MutablePropertySources maps = environment.getPropertySources();
if(maps != null){
Iterator<PropertySource<?>> its = maps.iterator();
while(its.hasNext()){
PropertySource<?> ps =its.next();
System.out.print(ps.getName() + "-----");
System.out.print(ps.getSource() + "-----");
System.out.println(ps.getClass() + "-----");
}
}
} }
运行结果
--------- execute MyApplicationStartingEventListener----------
servletConfigInitParams-----java.lang.Object@1ca2dfa-----class org.springframework.core.env.PropertySource$StubPropertySource-----
servletContextInitParams-----java.lang.Object@153f538-----class org.springframework.core.env.PropertySource$StubPropertySource-----
ApplicationPreparedEvent
ApplicationPreparedEvent:spring boot上下文context创建完成,但此时spring中的bean是没有完全加载完成的。在获取完上下文后,可以将上下文传递出去做一些额外的操作。值得注意的是:在该监听器中是无法获取自定义bean并进行操作的。
package com.ysl.listener; import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; public class MyApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent>{ @Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
ConfigurableApplicationContext context = event.getApplicationContext();
passContextInfo(context);
} /**
* 传递上下文
* @param cac
*/
private void passContextInfo(ApplicationContext cac) {
//dosomething()
}
}
ApplicationFailedEvent
ApplicationFailedEvent:spring boot启动异常时执行事件,在异常发生时,最好是添加虚拟机对应的钩子进行资源的回收与释放,能友善的处理异常信息
package com.ysl.listener; import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.ApplicationListener; public class MyApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> { @Override
public void onApplicationEvent(ApplicationFailedEvent applicationFailedEvent) {
Throwable t = applicationFailedEvent.getException();
//do something
} }
ApplicationReadyEvent
ApplicationReadyEvent:springboot 加载完成时候执行的事件
package com.ysl.listener; import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener; public class MyApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent>{
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
applicationReadyEvent.getApplicationContext();
System.out.println("start ready");
}
}
运行结果
-- ::54.453 INFO --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
-- ::54.513 INFO --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): (http)
start ready
-- ::54.517 INFO --- [ main] com.ysl.Application : Started Application in 4.718 seconds (JVM running for 5.264)
SpringBoot入门之事件监听的更多相关文章
- SpringBoot框架(6)--事件监听
一.场景:类与类之间的消息通信,例如创建一个对象前后做拦截,日志等等相应的事件处理. 二.事件监听步骤 (1)自定义事件继承ApplicationEvent抽象类 (2)自定义事件监听器,一般实现Ap ...
- SpringBoot事件监听机制及观察者模式/发布订阅模式
目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...
- springboot 中事件监听模型的一种实现
目录 定义事件本身 定义事件源 定义监听者 一.需要实现 ApplicationListener 二.使用 @EventListener 注解 测试 项目结构 前言: 事件监听模型是一种常用的设计模式 ...
- SpringBoot Application事件监听
SpringBoot Application共支持6种事件监听,按顺序分别是: ApplicationStartingEvent:在Spring最开始启动的时候触发 ApplicationEnviro ...
- SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)
SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...
- springBoot高级:自动配置分析,事件监听,启动流程分析,监控,部署
知识点梳理 课堂讲义 02-SpringBoot自动配置-@Conditional使用 Condition是Spring4.0后引入的条件化配置接口,通过实现Condition接口可以完成有条件的加载 ...
- SpringBoot的事件监听
事件监听的流程分为三步:1.自定义事件,一般是继承ApplicationEvent抽象类.2.定义事件监听器,一般是实现ApplicationListener接口.3.a.启动的时候,需要将监听器加入 ...
- 4.pygame快速入门-事件监听
事件event:游戏启动后,用户针对游戏的所有操作 监听:在游戏循环中,判断用户的具体操作 pygame中通过pygame.event.get()可以获得当前用户所做动作的事件列表 事件监听 wh ...
- Java中用得比较顺手的事件监听
第一次听说监听是三年前,做一个webGIS的项目,当时对Listener的印象就是个"监视器",监视着界面的一举一动,一有动静就触发对应的响应. 一.概述 通过对界面的某一或某些操 ...
随机推荐
- pyspider示例代码五:实现自动翻页功能
实现自动翻页功能 示例代码一 #!/usr/bin/env python # -*- encoding: utf- -*- # Created on -- :: # Project: v2ex fro ...
- 2018.07.25 hdu5306Gorgeous Sequence(线段树)
传送门 线段树基本操作. 要求维护区间取min" role="presentation" style="position: relative;"> ...
- spring boot打包后windows启动乱码
事情的起因什么的就不多表了,直接进入主题... 项目都要上线了,结果发现使用 idea mvn install之后的 jar在windows下启动乱码,而使用idea启动却没有问题!!! 这是神马情况 ...
- SDK | 声波传输
SDK | 声波传输 - 音频流生成 https://github.com/CloudSide/WaveTransSdk/tree/master/c/freq_util Objective-C: ht ...
- 2015 - 4- 21 iOS开发越狱环境的搭建1
2015 - 4- 20 1. 越狱环境的搭建 http://www.iduuke.com/2030.html http://www.cnblogs.com/xiongwj0910/archi ...
- vc中使用SendMessage正确发送自定义消息的方法
最近在用VC2008做开发,后来由于要用到消息的发送,而且需要自定义消息,在网上查找了很多例子,根据他们所说的,虽然大致都差不多,但是基本上没有 一个能完全做出来的.要知道VC编程有一个小地方出错,都 ...
- Codeforces735B Urbanization 2016-12-13 11:58 114人阅读 评论(0) 收藏
B. Urbanization time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces735A Ostap and Grasshopper 2016-12-13 11:53 78人阅读 评论(0) 收藏
A. Ostap and Grasshopper time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- D3 API总览
D3图形库API参考 https://github.com/d3/d3/wiki/API--%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C d3 官网 API https:/ ...
- WinRT 中后台任务类的声明
要实现后台任务,需要实现IBackgroundTask接口 public sealed class SimpleTask : IBackgroundTask { public void Run(IBa ...