When Does Spring Boot 2.0’s ApplicationStartedEvent Actually Fire?
This article explains the new ApplicationStartedEvent introduced in Spring Boot 2.0, shows the full event order, demonstrates how to write listeners for ApplicationPreparedEvent, ApplicationStartedEvent and ApplicationReadyEvent, and clarifies the difference between ApplicationStartedEvent and ApplicationReadyEvent using a CommandLineRunner example.
Spring Boot 2.0 enhances its event model by adding ApplicationStartedEvent. The complete event sequence is:
ApplicationStartingEvent ApplicationEnvironmentPreparedEvent ApplicationPreparedEvent ApplicationStartedEvent ApplicationReadyEvent ApplicationFailedEventTo observe where these events occur, three listeners are created. Each implements ApplicationListener for the corresponding event and logs a message when the event is received.
@Slf4j
public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationPreparedEvent event) {
log.info("......ApplicationPreparedEvent......");
}
}
@Slf4j
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
log.info("......ApplicationStartedEvent......");
}
}
@Slf4j
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
log.info("......ApplicationReadyEvent......");
}
}These listeners are registered via META-INF/spring.factories:
org.springframework.context.ApplicationListener=\
com.didispace.ApplicationPreparedEventListener,\
com.didispace.ApplicationReadyEventListener,\
com.didispace.ApplicationStartedEventListenerRunning the application produces log entries that clearly show the order of the events, for example:
2018-03-07 18:15:18.591 INFO 83387 --- [main] com.didispace.Application: Starting Application on ...
... (other startup logs) ...
2018-03-07 18:15:18.662 INFO 83387 --- [main] com.didispace.ApplicationPreparedEventListener: ......ApplicationPreparedEvent......
2018-03-07 18:15:18.846 INFO 83387 --- [main] com.didispace.ApplicationStartedEventListener: ......ApplicationStartedEvent......
2018-03-07 18:15:18.847 INFO 83387 --- [main] com.didispace.ApplicationReadyEventListener: ......ApplicationReadyEvent......The official documentation defines the two new events as follows:
An ApplicationStartedEvent is sent after the context has been refreshed but before any ApplicationRunner or CommandLineRunner have been called. An ApplicationReadyEvent is sent after those runners have been called, indicating that the application is ready to service requests.
To illustrate the gap between the two events, a CommandLineRunner bean is added that logs a message during its execution.
@Bean
public DataLoader dataLoader() {
return new DataLoader();
}
@Slf4j
static class DataLoader implements CommandLineRunner {
@Override
public void run(String... strings) throws Exception {
log.info("Loading data...");
}
}When the application is run again, the log shows the new runner output appearing between the ApplicationStartedEvent and ApplicationReadyEvent:
... ApplicationStartedEvent ...
... Loading data... // from CommandLineRunner
... ApplicationReadyEvent ...This demonstrates that ApplicationStartedEvent occurs right after the Spring context is prepared, while ApplicationReadyEvent marks the point where the application has finished executing all startup runners and is fully ready to handle requests.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
