The 30 second answer
SpringApplication.run first sets up bootstrap support and startup listeners, then prepares the Environment and loads configuration. After printing the banner, it creates and prepares the appropriate ApplicationContext. Refresh processes configuration and bean definitions, creates non-lazy singletons and starts an embedded server for web applications. Spring Boot then publishes ApplicationStartedEvent, runs ApplicationRunner and CommandLineRunner beans, and finally publishes ApplicationReadyEvent if startup succeeds. So started and ready aren't the same milestone.
The whole flow is visible in one method:
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.started(context, timeTakenToStarted);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
throw handleRunFailure(context, ex, listeners);
}
if (context.isRunning()) {
listeners.ready(context, startup.ready());
}SpringApplication.run L292-L330 (shortened)
Step by step:
- **Constructor (before
run).**SpringApplicationdeduces the web application type from the classpath and loads initializers and listeners fromspring.factories. - Starting.
ApplicationStartingEventis published. Almost nothing exists yet; logging is initialized here. - Environment.
prepareEnvironmentcreates theEnvironment, adds command-line arguments, and lets the config-data system loadapplication.properties/.yml, profile files and imports.ApplicationEnvironmentPreparedEventis published. - Context creation. The context type depends on the web type: servlet, reactive or plain.
- Prepare. Initializers run (
ApplicationContextInitializedEvent), the main class is registered as a bean source, thenApplicationPreparedEvent. - Refresh (Spring Framework's
AbstractApplicationContext.refresh()): bean factory post-processors run, which parse@Configurationclasses, component scanning and auto-configuration imports; bean post-processors are registered; for web contextsonRefresh()creates the embedded server; all non-lazy singletons are created;ContextRefreshedEventis published and the web server starts listening. - Started.
ApplicationStartedEvent, thenAvailabilityChangeEventwithLivenessState.CORRECT. - Runners. All
ApplicationRunnerandCommandLineRunnerbeans run, in@Orderorder. - Ready.
ApplicationReadyEvent, thenReadinessState.ACCEPTING_TRAFFIC.
If anything throws, handleRunFailure publishes ApplicationFailedEvent, reports the failure and closes the context.
Interview distinction: ContextRefreshedEvent (Spring Framework) fires before runners; ApplicationReadyEvent (Spring Boot) fires after them. Use ApplicationReadyEvent for "the app is fully up" work.
Alternate interview scenarios
"Spring Boot startup is taking 45 seconds. How do you diagnose and fix it?"
I measure before I change anything. Spring Boot can record startup steps if I set a BufferingApplicationStartup on SpringApplication, and the Actuator startup endpoint then shows each bean's creation time, so the slow beans are visible by name. Usually it is one of these: a bean doing I/O in its constructor or init method, such as warming a cache or calling another service; Flyway or Liquibase migrations; Hibernate validating or updating the schema; a connection pool that waits for a slow database; or wide component scanning with many unused auto-configurations. The fixes follow the finding. I move warm-up to a background task after startup, run migrations as a separate step, turn off schema validation in production, narrow the scan and exclude unneeded auto-configuration. spring.main.lazy-initialization hides the cost from startup, but it moves the cost and the errors to the first request.
Sources (4)
- Spring Boot reference: Application events and listenersdocs.spring.io
- Spring Boot reference: Application availabilitydocs.spring.io
- Spring Boot reference: SpringApplication (application startup tracking, lazy initialization)docs.spring.io
- Spring Boot reference: Actuator endpoints (startup)docs.spring.io