Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

List the main SpringApplication events in the order they are published during a successful startup of a web application.

A successful web application start publishes ApplicationStartingEvent, ApplicationEnvironmentPreparedEvent, ApplicationContextInitializedEvent and ApplicationPreparedEvent, then, during the context refresh, WebServerInitializedEvent and ContextRefreshedEvent. After the refresh come ApplicationStartedEvent and an AvailabilityChangeEvent with LivenessState.CORRECT, then the runners are called, and finally ApplicationReadyEvent and an AvailabilityChangeEvent with ReadinessState.ACCEPTING_TRAFFIC. If startup fails, ApplicationFailedEvent is published instead.

Why this question is asked

Startup events are how we hook our own code into the right moment of Spring Boot's start: add a property source before configuration is read, log something once the port is known, or announce that the service is ready. Choosing the wrong event is a common source of bugs, such as code that runs before the server accepts requests or a listener that never fires. Interviewers ask for the order because it shows we understand what exists at each moment. In this answer, we will walk through the events in order, say what is available at each one, see how to listen to the early ones, and look at the failure case.

1Before the Context Exists

The first four events happen before any bean is created, so they are mostly for framework and platform code. ApplicationStartingEvent is published at the very start of run(), before anything except listener registration and the bootstrap context. ApplicationEnvironmentPreparedEvent comes when the Environment, the holder of all properties and profiles, is ready, but no context exists yet; this is the moment to inspect or add property sources.

ApplicationContextInitializedEvent is published after the ApplicationContext has been created and the ApplicationContextInitializers have run, but before any bean definitions are loaded. ApplicationPreparedEvent comes after the bean definitions are loaded and just before the refresh starts. At this point the context is fully described but nothing is instantiated.

2During and After the Refresh

The refresh creates all singletons. Near its end, lifecycle beans start, and in a web application this starts the embedded server, which publishes WebServerInitializedEvent, a ServletWebServerInitializedEvent for a servlet app, carrying the actual port. Then the refresh completes and the Framework publishes ContextRefreshedEvent. Both happen inside the refresh, between ApplicationPreparedEvent and ApplicationStartedEvent.

After the refresh, Boot publishes ApplicationStartedEvent, followed by an AvailabilityChangeEvent setting liveness to CORRECT, meaning the application's internal state is fine. Then the ApplicationRunner and CommandLineRunner beans run. When they finish, Boot publishes ApplicationReadyEvent and then an AvailabilityChangeEvent setting readiness to ACCEPTING_TRAFFIC. Only then should a load balancer send requests.

SpringApplication events during a successful web start
OrderEventWhat exists at this point
1ApplicationStartingEventNothing but listeners
2ApplicationEnvironmentPreparedEventEnvironment and profiles, no context
3ApplicationContextInitializedEventContext created, initializers applied
4ApplicationPreparedEventBean definitions loaded, none created
5WebServerInitializedEventBeans created, server listening, port known
6ContextRefreshedEventRefresh complete
7ApplicationStartedEvent + LivenessState.CORRECTBefore runners
8(runners are called)ApplicationRunner, CommandLineRunner
9ApplicationReadyEvent + ReadinessState.ACCEPTING_TRAFFICReady to serve traffic
Events 5 and 6 are published inside the refresh; check your version for the exact relative order of those two.

3Seeing the Order for Ourselves

A small listener registered on the SpringApplication prints every event as it arrives. It must be added before run(), because the early events are published before any bean exists.

Java
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ShopApplication.class);
app.addListeners((ApplicationEvent e) -> {
if (e instanceof SpringApplicationEvent || e instanceof ContextRefreshedEvent
|| e instanceof WebServerInitializedEvent || e instanceof AvailabilityChangeEvent<?>) {
System.out.println(e.getClass().getSimpleName());
}
});
app.run(args);
}
// ApplicationStartingEvent
// ApplicationEnvironmentPreparedEvent
// ApplicationContextInitializedEvent
// ApplicationPreparedEvent
// ServletWebServerInitializedEvent
// ContextRefreshedEvent
// ApplicationStartedEvent
// AvailabilityChangeEvent (liveness CORRECT)
// ApplicationReadyEvent
// AvailabilityChangeEvent (readiness ACCEPTING_TRAFFIC)

Notice that if we turned this lambda into an @EventListener method on a bean, the first four lines would never appear. A bean listener only exists after the context starts creating beans, so it can only see events from the refresh onward. For early events, the listener must be added with addListeners or registered in META-INF/spring.factories under org.springframework.context.ApplicationListener.

4Choosing the Right Event

Each event fits a kind of work. Code that changes configuration belongs at the environment-prepared stage, usually as an EnvironmentPostProcessor rather than a listener. Code that needs the actual port, for example when server.port=0 picks a random one, listens to WebServerInitializedEvent. Code that announces the service to the outside world, sends a started message or opens the gates to traffic should wait for ApplicationReadyEvent, because only then have the runners finished.

A common mistake is using ContextRefreshedEvent for that last kind of work. It fires before the runners, and it fires again for every context refresh, including child contexts and test contexts. ApplicationReadyEvent or the readiness AvailabilityChangeEvent is the right signal for ready to serve.

5When Startup Fails

If anything throws during startup, in a listener, while creating a bean or in a runner, Boot publishes ApplicationFailedEvent instead of the remaining events. It carries the exception, so a listener can, for example, report the failure reason to a central system. After that, failure analyzers print the readable APPLICATION FAILED TO START message, the context is closed and the exception is rethrown from run().

A listener for ApplicationFailedEvent has the same early-registration rule if the failure can happen before beans exist, for example a bad property value. Registered through spring.factories, it catches failures from the very first step.

6Follow-up questions

Which event should I use to log the random port in a test?

WebServerInitializedEvent, through event.getWebServer().getPort(). In tests, @LocalServerPort injects the same value. ContextRefreshedEvent does not carry the port and may fire for other contexts.

Is ApplicationStartedEvent the same as ApplicationReadyEvent?

No. ApplicationStartedEvent is published after the refresh but before runners, and liveness becomes CORRECT then. ApplicationReadyEvent comes after the runners, and readiness becomes ACCEPTING_TRAFFIC. Long runners make the gap between them large.

Why do some events have two names, like WebServerInitializedEvent and ServletWebServerInitializedEvent?

WebServerInitializedEvent is the general type; a servlet application publishes the ServletWebServerInitializedEvent subtype and a reactive one publishes ReactiveWebServerInitializedEvent. Listening to the general type covers both.

7Conclusion

In this answer, we walked through the startup events in order: ApplicationStartingEvent, ApplicationEnvironmentPreparedEvent, ApplicationContextInitializedEvent and ApplicationPreparedEvent before beans exist; WebServerInitializedEvent and ContextRefreshedEvent during the refresh; ApplicationStartedEvent with liveness CORRECT; the runners; and ApplicationReadyEvent with readiness ACCEPTING_TRAFFIC. We saw that early events need listeners registered on SpringApplication or in spring.factories, and that ApplicationFailedEvent replaces the rest if startup fails.

What to say in the interviewAbout 70 seconds aloud

Open with

The order starts with ApplicationStartingEvent, right at the start of run. Then ApplicationEnvironmentPreparedEvent once the Environment and profiles are settled but there is no context yet. Then ApplicationContextInitializedEvent after the context is created and the initializers have run, and ApplicationPreparedEvent once bean definitions are loaded, just before the refresh.

Then, if they want more

During the refresh, all singletons are created, the embedded server starts and publishes WebServerInitializedEvent with the real port, and the refresh ends with ContextRefreshedEvent. After that Boot publishes ApplicationStartedEvent and an AvailabilityChangeEvent with LivenessState.CORRECT. Then the ApplicationRunner and CommandLineRunner beans run, and finally ApplicationReadyEvent followed by an AvailabilityChangeEvent with ReadinessState.ACCEPTING_TRAFFIC, which is the real ready to serve signal.

If anything fails, ApplicationFailedEvent is published with the exception and the context is closed. A practical point is that the first four events happen before any bean exists, so an @EventListener bean cannot see them; those listeners have to be added with addListeners or in spring.factories. And for anything like announcing the service, I would use ApplicationReadyEvent, not ContextRefreshedEvent.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "List the main SpringApplication events in the order they are published during a...": https://scrivio-interview-library.netlify.app/spring-boot/boot-startup/3b7049a7e7/