With @EventListener, we can quickly turn a component's method into an event listener. Spring Boot has some predefined events we can tap into; they mark milestones in the application lifecycle.
- ApplicationStartingEvent
Only the bootstrap context has been created, and "java.awt.headless" is turned off unless explicitly enabled externally. - ApplicationEnvironmentPreparedEvent
The environment sources are parsed and configured.
Now you still have the opportunity to alter the environment, as the binding is a later step. - ApplicationContextInitializedEvent
Context is prepared, the environment is applied, and initializers have been executed. After this event, the bootstrap context is discarded. - ApplicationPreparedEvent
Beans were loaded, the magic happened. - ApplicationStartedEvent
The application context is refreshed, and we're alive.
After this event is published, the liveness state is set to "correct" since there were no failures during initialization. - On error between steps 2-5, an ApplicationFailedEvent is published before the shutdown hook is executed.
- ApplicationReadyEvent
The startup is done, measurements are taken, startup info is logged, and runners are executed (command line or job). The readiness state is set to "accepting traffic" to let external actors know we're online.