转载

springboot源码分析16-spring boot监听器使用

摘要: spring boot提供了一系列的监听器,方便我们开发人员使用和扩展。

本文咱们详细讲解一下 spring boot中的监听器。

spring boot中支持的事件类型定在org.springframework.boot.context.event包中,目前支持的事件类型有如下6种:

ApplicationFailedEvent

ApplicationPreparedEvent

ApplicationReadyEvent

ApplicationStartedEvent Springboot2.x版本已修改为 ApplicationStartingEvent

SpringApplicationEvent

ApplicationEnvironmentPreparedEvent

1.1.  监听器的使用

第一:首先定义一个自己使用的监听器类并实现 ApplicationListener接口。

第二:通过 SpringApplication类中的addListeners方法将自定义的监听器注册进去。

1.1.1.  ApplicationFailedEvent

ApplicationFailedEvent :该事件为 spring boot 启动失败时的操作。

/**

* spring boot 启动的时候出现异常事件

* @author www.shareniu.com

*

*/

public class ShareniuApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {

@Override

public void onApplicationEvent(ApplicationFailedEvent event) {

System.out.println("--------------:ShareniuApplicationFailedEventListener");

Throwable exception = event.getException();

System.out.println(exception);

}

可以通过 ApplicationFailedEvent 获取 Throwable 实例对象获取异常信息并处理。

1.1.2.  ApplicationPreparedEvent

ApplicationPreparedEvent :上下文准备事件。

上下文 context 已经准备完毕 ,可以通过 ApplicationPreparedEvent 获取到 ConfigurableApplicationContext 实例对象。 ConfigurableApplicationContext 类继承 ApplicationContext 类, 但需要注意这个时候 spring 容器中的 bean 还没有被完全的加载 ,因此如果通过 ConfigurableApplicationContext 获取 bean 会报错的。比如报错:

Exception in thread "main" java.lang.IllegalStateException: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@69b0fd6f has not been refreshed yet

获取到上下文之后,可以将其注入到其他类中,毕竟 ConfigurableApplicationContext 为引用类型。

public class ShareniuApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {

@Override

public void onApplicationEvent(ApplicationPreparedEvent event) {

System.out.println("###############"+"ShareniuApplicationPreparedEventListener");

ConfigurableApplicationContext applicationContext = event.getApplicationContext();

// 如果执行下面代码则报错

//ShareniuDemo shareniuDemo = applicationContext.getBean(ShareniuDemo.class);

//System.out.println(shareniuDemo);

}

1.1.3.  ApplicationReadyEvent

ApplicationReadyEvent :上下文已经准备 ok

这个时候就可以通过 ApplicationReadyEvent 获取 ConfigurableApplicationContext ,然后通过 ConfigurableApplicationContext 获取 bean 的信息。

public class ShareniuApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

@Override

public void onApplicationEvent(ApplicationReadyEvent event) {

System.out.println("--------------------:ShareniuApplicationReadyEventListener");

ConfigurableApplicationContext applicationContext = event.getApplicationContext();

//ShareniuDemo 可以根基自身情况进行测试

ShareniuDemo shareniuDemo = applicationContext.getBean(ShareniuDemo.class);

}

1.1.4.  ApplicationStartedEvent

ApplicationStartedEvent spring boot 启动监听类。该类在 SpringBoot2.x 版本中已经废弃,修改为了最新的类,类名是 ApplicationStartingEvent 。这个事件是第一个产生的。

可以在 SpringApplication 启动之前做一些手脚,比如修改 SpringApplication 实例对象中的属性值。

public class ShareniuApplicationStartedEventListener  implements ApplicationListener<ApplicationStartedEvent>{

@Override

public void onApplicationEvent(ApplicationStartedEvent event) {

SpringApplication springApplication = event.getSpringApplication();

springApplication.setShowBanner(false);

System.out.println("##############################ShareniuApplicationStartedEventListener");

}

1.1.5.  SpringApplicationEvent

SpringApplicationEvent :获取 SpringApplication

public class ShareniuSpringApplicationEventListener implements ApplicationListener<SpringApplicationEvent> {

@Override

public void onApplicationEvent(SpringApplicationEvent event) {

System.out.println("-----------------------:ShareniuSpringApplicationEventListener");

SpringApplication springApplication = event.getSpringApplication();

System.out.println("###############"+springApplication);

}

1.1.6.  ApplicationEnvironmentPreparedEvent

ApplicationEnvironmentPreparedEvent :环境事先准备, spring boot 中的环境已经准备 ok

可以通过 ApplicationEnvironmentPreparedEvent 获取到 SpringApplication ConfigurableEnvironment 等等信息, 可以通过 ConfigurableEnvironment 实例对象来修改以及获取默认的环境信息。

public class ShasreniuApplicationEnvironmentPreparedEventListener  implements ApplicationListener<ApplicationEnvironmentPreparedEvent>{

@Override

public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {

System.out.println("###############"+"ShasreniuApplicationEnvironmentPreparedEventListener");

SpringApplication springApplication = event.getSpringApplication();

ConfigurableEnvironment environment = event.getEnvironment();

long timestamp = event.getTimestamp();

Object source = event.getSource();

System.out.println("########################"+springApplication);

System.out.println("########################"+environment);

System.out.println("########################"+timestamp);

System.out.println("########################"+source);

MutablePropertySources propertySources = environment.getPropertySources();

if (propertySources!=null) {

Iterator<PropertySource<?>> iterator = propertySources.iterator();

while (iterator.hasNext()) {

PropertySource<?> propertySource = (PropertySource<?>) iterator.next();

System.out.println("##############:propertySource"+propertySource);

}

}

1.2.  监听器注册

@RestController

@SpringBootApplication()

public class Application {

@RequestMapping("/")

String index() {

return "xxxxxxxxxxxxx";

}

public static void main(String[] args) {

SpringApplication springApplication = new SpringApplication(Application.class);

springApplication.addListeners(new ShareniuApplicationStartedEventListener());

springApplication.addListeners(new ShasreniuApplicationEnvironmentPreparedEventListener());

springApplication.addListeners(new ShareniuApplicationPreparedEventListener());

springApplication.addListeners(new ShareniuApplicationFailedEventListener());

springApplication.addListeners(new ShareniuApplicationReadyEventListener());

springApplication.addListeners(new ShareniuSpringApplicationEventListener());

springApplication.run(args);

}

}

欢迎关注我的微信公众号,第一时间获得博客更新提醒,以及更多 成体系 的Java相关原创技术干货。 
扫一扫下方二维码或者长按识别二维码,即可关注。

springboot源码分析16-spring boot监听器使用

原文  http://blog.csdn.net/qq_30739519/article/details/78830773
正文到此结束
Loading...