23.5 Application events and listeners Application 的事件监听器

除了使用Spring框架的事件之外,SpringApplication也会发送一些额外的事件。

有些事件是在 ApplicationContext 生成之前就自动触发了,对于这些事件,不能作为 @Bean 注册监听器,可以通过 SpringApplication.addListeners(…​) or SpringApplicationBuilder.listeners(…​) 方法。

代码和结果如下:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
   
SpringApplication application = new SpringApplication(Application.class);
application.addListeners(new ApplicationListenerStarted());
application.addListeners(new ApplicationListenerEnvironmentPrepared());
application.addListeners(new ApplicationListenerPrepared());
application.addListeners(new ApplicationListenerReadyEvent());
application.addListeners(new ApplicationListenerFailed());
application.run(args);
}} class ApplicationListenerEnvironmentPrepared implements ApplicationListener<ApplicationEnvironmentPreparedEvent> { @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { System.out.println("--------------------0"+getClass().getSimpleName()); }} class ApplicationListenerFailed implements ApplicationListener<ApplicationFailedEvent> { @Override public void onApplicationEvent(ApplicationFailedEvent event) { System.out.println("--------------------1"+getClass().getSimpleName()); }} class ApplicationListenerPrepared implements ApplicationListener<ApplicationPreparedEvent> { @Override public void onApplicationEvent(ApplicationPreparedEvent event) { System.out.println("--------------------2"+getClass().getSimpleName()); }}class ApplicationListenerStarted implements ApplicationListener<ApplicationStartedEvent> { @Override public void onApplicationEvent(ApplicationStartedEvent event) { System.out.println("--------------------3"+getClass().getSimpleName()); }

执行结果



2017-06-10 14:28:43.932  INFO 25636 --- [  restartedMain] com.csg.Application                      : Started Application in 0.346 seconds (JVM running for 85.356)
2017-06-10 14:29:03.134  INFO 25636 --- [      Thread-18] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1d48ba6f: startup date [Sat Jun 10 14:28:43 CST 2017]; root of context hierarchy
2017-06-10 14:29:03.134  INFO 25636 --- [      Thread-18] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
--------------------0ApplicationListenerStarted
--------------------1ApplicationListenerEnvironmentPrepared
┬┴┬/ ̄\_/ ̄\
┬┴┬┴▏  ▏▔▔▔▔\
┴┬┴/\ /      ﹨
┬┴∕       /   )
┴┬▏        ●  ▏
┬┴▏           ▔█ 
┴◢██◣      \__/   !
┬█████◣       /  
┴█████████████◣
◢██████████████▆▄
█◤◢██◣◥█████████◤\
◥◢████ ████████◤   \
┴█████ ██████◤      ﹨
┬│   │█████◤         ▏
┴│   │               ▏
┬∕   ∕    /▔▔▔\     ∕
┴/___/﹨   ∕     ﹨  /\


#这个是springboot的版本号
1.5.4.RELEASE
#这个是springboot的版本号
 (v1.5.4.RELEASE)
2017-06-10 14:29:03.289  INFO 25636 --- [  restartedMain] com.csg.Application                      : Starting Application on DESKTOP-F36IHM2 with PID 25636 (J:\work1\target\classes started by csg in J:\work1)
2017-06-10 14:29:03.289  INFO 25636 --- [  restartedMain] com.csg.Application                      : No active profile set, falling back to default profiles: default
--------------------2ApplicationListenerPrepared
2017-06-10 14:29:03.291  INFO 25636 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@22e8c4f7: startup date [Sat Jun 10 14:29:03 CST 2017]; root of context hierarchy
2017-06-10 14:29:03.460  INFO 25636 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 1111 (http)
2017-06-10 14:29:03.461  INFO 25636 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2017-06-10 14:29:03.461  INFO 25636 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-06-10 14:29:03.469  INFO 25636 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-06-10 14:29:03.469  INFO 25636 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 179 ms
2017-06-10 14:29:03.484  INFO 25636 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-10 14:29:03.484  INFO 25636 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-10 14:29:03.484  INFO 25636 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-10 14:29:03.484  INFO 25636 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-10 14:29:03.484  INFO 25636 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-10 14:29:03.515  INFO 25636 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@22e8c4f7: startup date [Sat Jun 10 14:29:03 CST 2017]; root of context hierarchy
2017-06-10 14:29:03.518  INFO 25636 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-06-10 14:29:03.519  INFO 25636 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-06-10 14:29:03.523  INFO 25636 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-10 14:29:03.523  INFO 25636 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-10 14:29:03.527  INFO 25636 --- [  restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-10 14:29:03.536  INFO 25636 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2017-06-10 14:29:03.554  INFO 25636 --- [  restartedMain] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-06-10 14:29:03.561  INFO 25636 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 1111 (http)
--------------------3ApplicationListenerReadyEvent
2017-06-10 14:29:03.562  INFO 25636 --- [  restartedMain] com.csg.Application                      : Started Application in 0.292 seconds (JVM running for 104.986)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值