天天看點

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[email protected]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[email protected]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: [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)

繼續閱讀