Understanding Spring Boot Application Events: A Complete Lifecycle Guide
This article outlines the full sequence of Spring Boot application events—from ApplicationStartingEvent at launch to ApplicationFailedEvent on errors—explaining when each event fires, its purpose, and providing code snippets that illustrate the event triggers within the SpringApplication lifecycle.
During runtime, Spring Boot sends application events in the following order:
1 ApplicationStartingEvent
Sent at the very beginning of the run, before any processing.
2 ApplicationEnvironmentPreparedEvent
Sent when the environment (configuration properties) is known but before the context is created.
3 ApplicationContextInitializedEvent
Sent after the ApplicationContext is prepared and ApplicationContextInitializers are called, but before any bean definitions are loaded.
<code>public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
// ...
// Triggers ApplicationEnvironmentPreparedEvent before context initialization
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
// Core method that instantiates beans, etc.
refreshContext(context);
// ...
}
private void prepareContext(...) {
// ...
listeners.contextPrepared(context);
// ...
}
}
</code>4 ApplicationPreparedEvent
Sent just before the refresh starts, after bean definitions have been loaded.
<code>public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
// ...
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
// Event is sent in this method
listeners.contextLoaded(context);
}
}
</code>5 ApplicationStartedEvent
Sent after the context is refreshed and before any application and command‑line runners are invoked.
<code>public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
// ...
refreshContext(context);
// Triggers ApplicationStartedEvent here
listeners.started(context);
// ...
}
}
</code>6 AvailabilityChangeEvent (Liveness)
Immediately after, a LivenessState.CORRECT event is published to indicate the application is alive.
<code>public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
// ...
refreshContext(context);
listeners.started(context);
// ...
}
}
</code>7 ApplicationReadyEvent
Sent after all application and command‑line runners have been called, indicating the app is ready to service requests.
<code>public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
// ...
refreshContext(context);
try {
// Triggers ApplicationReadyEvent here
listeners.running(context);
} catch (Throwable ex) {
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}
}
</code>8 AvailabilityChangeEvent (Readiness)
Sent right after with ReadinessState.ACCEPTING to signal the application is ready to accept traffic.
<code>public void running(ConfigurableApplicationContext context) {
context.publishEvent(new ApplicationReadyEvent(this.application, this.args, context));
AvailabilityChangeEvent.publish(context, ReadinessState.ACCEPTING_TRAFFIC);
}
</code>35.9 ApplicationFailedEvent
Published if an exception occurs during startup.
<code>public class SpringApplication {
public ConfigurableApplicationContext run(String... args) {
// ...
try {
refreshContext(context);
// ...
} catch (Throwable ex) {
// Triggers ApplicationFailedEvent here
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
// ...
}
}
</code>Additional events bound to SpringApplication include WebServerInitializeEvent (sent when the embedded web server is ready) and ContextRefreshedEvent (sent after the ApplicationContext refresh completes).
WebServerInitializeEvent
Sent after the WebServer is prepared.
<code>public abstract class AbstractApplicationContext {
public void refresh() {
finishRefresh();
}
protected void finishRefresh() {
// Initialize lifecycle beans
initLifecycleProcessor();
// Triggers WebServerInitializeEvent here (e.g., Tomcat startup)
getLifecycleProcessor().onRefresh();
// ...
}
}
</code>ContextRefreshedEvent
Sent when the ApplicationContext refresh finishes.
<code>public abstract class AbstractApplicationContext {
public void refresh() {
finishRefresh();
}
protected void finishRefresh() {
// ...
// Triggers ContextRefreshedEvent here
publishEvent(new ContextRefreshedEvent(this));
// ...
}
}
</code>Images illustrating the event flow:
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.