Spring Boot WebFlux Application Startup Sequence
September 3, 2019
Abstract
This article describes the activities that happen and the events that occur when a Spring Boot WebFlux application starts.
2. Event listeners
Applications can react to events published by the Spring framework by registering listeners.
MyEventListener.java
package rahulb.experiments.springappstartup.listener;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
public class MyEventListener implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// Handle the event
System.out.printf("Received %s%n", event.getClass().getSimpleName());
}
}
META-INF/spring.factories
org.springframework.context.ApplicationListener=\
rahulb.experiments.springappstartup.listener.MyEventListener
3. Sample console logs
If the event listeners, beans and application runners print logs on the console, the console log would appear as follows:
The timestamp, severity, thread name, PID, logger name, etc. have been deleted from the log for brevity. |
Console log
Received ApplicationStartingEvent (1)
Received ApplicationEnvironmentPreparedEvent
:: Spring Boot :: (v2.1.7.RELEASE)
Received ApplicationContextInitializedEvent
Starting Application on xxx with PID xxxxx ...
No active profile set, falling back to default profiles: default
Received ApplicationPreparedEvent
Controller.init() was called (2)
Received ContextRefreshedEvent (3)
o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080 (4)
Received ReactiveWebServerInitializedEvent (5)
Started Application in 10.574 seconds (JVM running for 10.953)
Received ApplicationStartedEvent (6)
ApplicationRunner.run(...) was called
Received ApplicationReadyEvent (7)
1 | ApplicationStartingEvent is published even before the loggers are ready. |
2 | The beans are instantiated and their init method is invoked after ApplicationPreparedEvent . |
3 | ContextRefreshedEvent is published after the beans are instantiated and initialized. |
4 | The web server is started after ContextRefreshedEvent . |
5 | ReactiveWebServerInitializedEvent is published after the web server is started. |
6 | ApplicationStartedEvent is published before the application runners are run. |
7 | ApplicationReadyEvent is published after the application runners are run. |