spring boot在启动过程当中增长事件监听机制,为用户功能拓展提供极大的便利,sptingboot支持的事件类型有如下五种:java
实现监听步骤spring
1.监听类实现ApplicationListener接口springboot
2.将监听类添加到SpringApplication实例中app
ApplicationStartingEvent:springboot启动开始的时候执行的事件,在该事件中能够获取到SpringApplication
对象,可作一些执行前的设置。ide
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); } }
执行结果以下:spa
--------- 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:spring boot 对应Enviroment已经准备完毕,但此时上下文context尚未建立。在该监听中获取到ConfigurableEnvironment
后能够对配置信息作操做,例如:修改默认的配置信息,增长额外的配置信息等等。code
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:spring boot上下文context建立完成,但此时spring中的bean是没有彻底加载完成的。在获取完上下文后,能够将上下文传递出去作一些额外的操做。值得注意的是:在该监听器中是没法获取自定义bean并进行操做的。blog
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: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: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)