Scrivio Interview library
InternalsFoundations5 min read · updated 12 November 2026

Describe what SpringApplication.run does from the call in main until the application is ready.

SpringApplication.run first deduces the application type (servlet, reactive or none) and loads initializers and listeners from spring.factories. It then prepares the Environment with property sources and profiles, prints the banner, creates the ApplicationContext, applies the initializers, loads the bean definitions and refreshes the context, which creates the singletons and, for a web app, starts the embedded server. Finally it calls the ApplicationRunner and CommandLineRunner beans and publishes ApplicationReadyEvent.

Why this question is asked

One line in main starts everything in a Spring Boot application, so it is easy to treat it as a black box. Knowing what happens inside it pays off when something goes wrong at startup: a property that is not picked up, a listener that never fires, a slow start or a server that accepts traffic too early. Interviewers ask it because the order of these steps explains most startup puzzles. In this answer, we will follow run() from the constructor to the ready event, see what each phase does and why it comes where it does, and note which events mark each step.

1Creating the SpringApplication

The static SpringApplication.run(App.class, args) is a shortcut for new SpringApplication(App.class).run(args). The constructor already does real work. It deduces the web application type from the classpath: if Spring MVC's DispatcherServlet is present the type is SERVLET, if only WebFlux is present it is REACTIVE, and otherwise NONE. This decides which kind of ApplicationContext is created later.

The constructor also loads BootstrapRegistryInitializer, ApplicationContextInitializer and ApplicationListener implementations listed in META-INF/spring.factories files on the classpath. The reason they come from spring.factories and not from beans is timing: these objects are needed before any bean exists. Finally it finds the class that holds main, which is used for logging.

Java
public static void main(String[] args) {
SpringApplication.run(OrdersApplication.class, args);
// same as: new SpringApplication(OrdersApplication.class).run(args)
// constructor: webApplicationType = SERVLET (spring-webmvc on classpath)
// initializers and listeners loaded from META-INF/spring.factories
}

2Preparing the Environment

run() starts a timer, creates a small bootstrap context for early services, and gets the SpringApplicationRunListeners, the objects that turn each phase into an event. It publishes ApplicationStartingEvent, then builds the Environment, the object that holds all properties and profiles.

Building the Environment means collecting property sources in their order of precedence: command-line arguments, system properties, environment variables, and the application.properties or application.yml files with their profile-specific variants. Those files are loaded by the config data environment post-processor, which reacts to ApplicationEnvironmentPreparedEvent. Active profiles are settled here too. Notice the consequence: everything that decides configuration happens before a single bean is created, which is why a listener that wants to add property sources must run at this stage, not as a bean.

After the Environment is ready, Boot prints the banner. That is why the banner can use properties such as the application name and version.

3Creating and Preparing the Context

Next Boot creates the ApplicationContext that matches the type. For a servlet application that is an AnnotationConfigServletWebServerApplicationContext, a context that knows how to create an embedded web server. Boot attaches the Environment to it and applies every ApplicationContextInitializer, which can customize the context before beans are loaded; then it publishes ApplicationContextInitializedEvent.

Then it loads the sources, which usually means registering our main class as a bean definition. Only the definitions are registered here, the recipes for beans, not the beans themselves. Boot publishes ApplicationPreparedEvent at the end of this phase. At this point the context is fully described but nothing has been instantiated.

Step throughSpringApplication.run from main to ready
  1. Construct SpringApplicationdeduce type, load initializers and listeners
  2. Prepare Environmentproperty sources, profiles
  3. Print banner
  4. Create and prepare contextinitializers, register main class
  5. Refresh contextscan, auto-config, create singletons, start server
  6. Call runnersApplicationRunner, CommandLineRunner
  7. Publish ApplicationReadyEventreadiness: ACCEPTING_TRAFFIC

7 steps in order. Play them, or step through them one at a time.

4Refreshing the Context

refresh() is the core of Spring Framework and the longest phase. First it runs the bean factory post-processors. The most important is ConfigurationClassPostProcessor, which reads our @SpringBootApplication class, performs component scanning and processes @EnableAutoConfiguration, so the auto-configuration candidates from the AutoConfiguration.imports files are evaluated against their conditions here. The result is the complete set of bean definitions.

Then it registers bean post-processors, the hooks that create proxies for @Transactional or @Async and process @Autowired. In onRefresh, a web context creates the embedded server, for example Tomcat, but does not yet accept connections. Next every non-lazy singleton is created, which is usually where startup time goes. Finally, at the end of refresh, lifecycle beans are started, which starts the web server listening on its port, and ContextRefreshedEvent is published.

So by the end of refresh, the server is up and the port is open. That detail matters: the application can receive requests before the next steps have finished, which is why readiness is tracked separately.

5Calling Runners and Becoming Ready

After refresh, Boot logs the familiar line with the start time and publishes ApplicationStartedEvent, followed by an AvailabilityChangeEvent setting liveness to CORRECT. Then it calls every ApplicationRunner and CommandLineRunner bean in order, on the main thread.

Java
// typical log, in order
// Tomcat initialized with port 8080 (http)
// Root WebApplicationContext: initialization completed in 812 ms
// Tomcat started on port 8080 (http) with context path '/'
// Started OrdersApplication in 2.41 seconds (process running for 2.7)
// ... runners run here ...
// (ApplicationReadyEvent, readiness ACCEPTING_TRAFFIC)

When the runners return, Boot publishes ApplicationReadyEvent and then an AvailabilityChangeEvent setting readiness to ACCEPTING_TRAFFIC. Only now does the readiness probe report that the service should get traffic. If any step throws an exception, Boot publishes ApplicationFailedEvent, runs failure analyzers to print a helpful message, closes the context and rethrows, so the application exits.

6Follow-up questions

Why are initializers and listeners loaded from spring.factories instead of being beans?

Because they are needed before the bean factory exists. Listeners must receive ApplicationStartingEvent and ApplicationEnvironmentPreparedEvent, and initializers must customize the context before beans are loaded. Beans only exist after refresh, which is too late.

Where are auto-configurations evaluated in this sequence?

During refresh, when ConfigurationClassPostProcessor processes @EnableAutoConfiguration. Boot reads the candidates from the AutoConfiguration.imports files and evaluates their conditions after our own configuration has been read, so @ConditionalOnMissingBean can see our beans.

Can I stop the server from accepting traffic until runners finish?

The port is already open after refresh, but readiness stays REFUSING_TRAFFIC until ApplicationReadyEvent. In Kubernetes, the readiness probe on /actuator/health/readiness keeps traffic away until then, which is the intended way to handle it.

7Conclusion

In this answer, we followed SpringApplication.run from the constructor, which deduces the application type and loads initializers and listeners from spring.factories, through preparing the Environment and printing the banner, creating and preparing the context, and refreshing it, where auto-configuration is evaluated, singletons are created and the embedded server starts. Finally the runners are called and ApplicationReadyEvent marks the application ready to serve traffic.

What to say in the interviewAbout 70 seconds aloud

Open with

In the constructor, SpringApplication deduces the application type from the classpath, servlet, reactive or none, and loads the bootstrap initializers, context initializers and application listeners from spring.factories, because they are needed before any beans exist.

Then, if they want more

Then run starts: it publishes ApplicationStartingEvent, prepares the Environment, which means collecting command-line arguments, environment variables and the application.yml files including profile-specific ones, and settles the active profiles. Then it prints the banner.

Next it creates the right context type, for a servlet app one that can start an embedded server, applies the initializers and registers the main class as a bean definition.

Then comes refresh, the Spring Framework core: ConfigurationClassPostProcessor does component scanning and evaluates the auto-configurations against their conditions, bean post-processors are registered, the embedded server is created, all non-lazy singletons are instantiated, and at the end the server starts listening and ContextRefreshedEvent is published.

After refresh, Boot publishes ApplicationStartedEvent and liveness CORRECT, calls the ApplicationRunner and CommandLineRunner beans on the main thread, and finally publishes ApplicationReadyEvent and readiness ACCEPTING_TRAFFIC. If anything fails, failure analyzers print the message and the context is closed.

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 "Describe what SpringApplication.run does from the call in main until the application is...": https://scrivio-interview-library.netlify.app/spring-boot/boot-startup/3ded0f62a0/