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---------- 2018-03-03 11:00:58.027 INFO 7644 --- [ main] com.ysl.Application : Starting Application on master with PID 7644 (/home/workspace/springboottest/target/classes started by ysl in /home/workspace/springboottest) 2018-03-03 11:00:58.032 INFO 7644 --- [ main] com.ysl.Application : No active profile set, falling back to default profiles: default 2018-03-03 11:00:58.182 INFO 7644 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f32bf7: startup date [Sat Mar 03 11:00:58 CST 2018]; 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"); } }
运行结果
2018-03-03 11:19:54.453 INFO 8478 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-03-03 11:19:54.513 INFO 8478 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) start ready 2018-03-03 11:19:54.517 INFO 8478 --- [ main] com.ysl.Application : Started Application in 4.718 seconds (JVM running for 5.264)