Application events are sent in the following order, as your application runs:
- An ApplicationStartingEvent is sent at the start of a run but before any processing, except for
the registration of listeners and initializers. - An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is
known but before the context is created. - An ApplicationContextInitializedEvent is sent when the ApplicationContext is prepared and
ApplicationContextInitializers have been called but before any bean definitions are loaded. - An ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions
have been loaded. - An ApplicationStartedEvent is sent after the context has been refreshed but before any
application and command-line runners have been called. - An ApplicationReadyEvent is sent after any application and command-line runners have been
called. It indicates that the application is ready to service requests.
50 - An ApplicationFailedEvent is sent
example:
Listener
public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {
public void onApplicationEvent(ApplicationPreparedEvent event) {
System.out.println("......ApplicationPreparedEvent......");
}
}
Main
public static void main(String[] args) {
// SpringApplication.run(DemoApplication.class, args);
SpringApplication springApplication = new SpringApplication(DemoApplication.class);
springApplication.addListeners(new ApplicationPreparedEventListener());
springApplication.run(args);
}
输出:
2020-04-04 00:07:15.888 INFO 9672 --- [ main] com.github.examples.DemoApplication : Starting DemoApplication on DESKTOP-GLVVPCF with PID 9672 (D:\03github\springboot2-learning\chapter3\target\classes started by limiao in D:\03github\springboot2-learning)
2020-04-04 00:07:15.910 INFO 9672 --- [ main] com.github.examples.DemoApplication : No active profile set, falling back to default profiles: default
......ApplicationPreparedEvent......
2020-04-04 00:07:18.364 INFO 9672 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-04-04 00:07:18.383 INFO 9672 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-04 00:07:18.384 INFO 9672 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-04 00:07:18.544 INFO 9672 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-04 00:07:18.544 INFO 9672 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2457 ms
2020-04-04 00:07:18.906 INFO 9672 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-04 00:07:19.011 INFO 9672 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [public/index.html]
2020-04-04 00:07:19.202 INFO 9672 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-04-04 00:07:19.207 INFO 9672 --- [ main] com.github.examples.DemoApplication : Started DemoApplication in 4.214 seconds (JVM running for 6.12)
网友评论