What does SpringApplication.run(...) do when a Spring Boot application starts?
answer
- one line in main() bootstraps everything
- primary source = @SpringBootApplication class
- infer type -> Environment -> context -> refresh
- refresh starts embedded server
- returns ConfigurableApplicationContext
basics
~10 sSpringApplication.run() bootstraps the app: it creates and configures the Spring ApplicationContext, runs auto-configuration, starts any embedded web server, and returns the running context. It's the one line called from main().
solid answer
~40 sSpringApplication.run(MyApp.class, args) is the entry point Spring Boot's main() calls. It creates a SpringApplication for the given primary source (usually your @SpringBootApplication class), then bootstraps the whole app: it decides the application type (servlet/reactive/none), creates the matching ApplicationContext, prepares the Environment (loading application.properties/yaml, profiles, command-line args), prints the banner, applies ApplicationContextInitializers, registers bean definitions, and finally refreshes the context. Refresh instantiates singletons, runs auto-configuration, and starts an embedded web server (Tomcat by default) if it's a web app. It fires lifecycle events throughout (ApplicationStartingEvent, EnvironmentPreparedEvent, ContextRefreshedEvent, ApplicationReadyEvent) and calls any ApplicationRunner/CommandLineRunner beans. It returns the ConfigurableApplicationContext so you can inspect beans if needed.
code
java · 9 lines@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext ctx =
SpringApplication.run(DemoApplication.class, args);
// ctx is the running context; usually you don't need to touch it
System.out.println("Beans loaded: " + ctx.getBeanDefinitionCount());
}
}go deeper
Should know it bootstraps the ApplicationContext and starts the embedded server, called from main().
Should describe Environment prep, refresh, and the lifecycle events fired.
Should articulate the ordered phases and where auto-config and server start happen.
Should connect run() phases to extension points (initializers, listeners, spring.factories) and startup performance.
## What it is `SpringApplication` is Spring Boot's bootstrap class. The static `SpringApplication.run(Class<?> primarySource, String... args)` is a convenience that does `new SpringApplication(primarySource).run(args)`. It is the single call your `public static void main` uses to launch the whole application. ## The primary source The class you pass (typically the one annotated with `@SpringBootApplication`) is the **primary source** of bean definitions. `@SpringBootApplication` is itself a meta-annotation combining `@SpringBootConfiguration` (a `@Configuration`), `@EnableAutoConfiguration` (turns on Boot's auto-configuration), and `@ComponentScan` (scans the package of that class and below). So passing that one class tells Boot where to scan and to enable auto-config. ## What run() does, step by step 1. **Create the `SpringApplication`** and infer the **web application type** by inspecting the classpath (SERVLET, REACTIVE, or NONE). 2. **Fire `ApplicationStartingEvent`** via registered `ApplicationListener`s and `SpringApplicationRunListener`s (discovered from `META-INF/spring.factories` / `spring/…imports`). 3. **Prepare the `Environment`**: bind command-line `args`, system properties, OS env vars, and load `application.properties`/`application.yml`, activate profiles. Fires `ApplicationEnvironmentPreparedEvent`. 4. **Print the banner** (`Banner.Mode`). 5. **Create the `ApplicationContext`** matching the web type (`AnnotationConfigServletWebServerApplicationContext`, `AnnotationConfigReactiveWebServerApplicationContext`, or `AnnotationConfigApplicationContext`). 6. **Prepare the context**: apply `ApplicationContextInitializer`s, register the primary sources as bean definitions, fire `ApplicationContextInitializedEvent` and `ApplicationPreparedEvent`. 7. **Refresh the context** (`AbstractApplicationContext.refresh()`): this is the heavy part — process `@Configuration`, run all auto-configuration, instantiate singleton beans, and **start the embedded web server** for a web app. Fires `ContextRefreshedEvent`. 8. **Call runners**: every `ApplicationRunner` and `CommandLineRunner` bean is invoked. 9. **Fire `ApplicationReadyEvent`** — the app is fully up. If anything throws, `ApplicationFailedEvent` fires and the exception propagates. ## Return value `run` returns a `ConfigurableApplicationContext`. Most apps ignore it, but you can capture it to look up beans programmatically, e.g. in tests or CLI tools. ## Gotchas - The embedded server starts **during refresh**, not after — so a `ContextRefreshedEvent` listener already sees the server listening. - `SpringApplication.run` blocks only in the sense that startup is synchronous; the JVM stays alive because a web app has non-daemon server threads. A `NONE` (non-web) app will exit after `run` returns unless something keeps it alive. - Passing the wrong class (one without component scanning) means no beans get found.
- What is the 'primary source' you pass to run()?The bean-definition source Boot uses to start scanning and configuring — normally the @SpringBootApplication class, which enables component scanning and auto-configuration from its package.
- At what point does the embedded Tomcat start?During context refresh (step inside AbstractApplicationContext.refresh), not as a separate step after run() — a WebServerApplicationContext starts the server as part of refreshing.
saying these in an interview costs you the question
- Saying run() only 'starts Tomcat' and ignoring context creation/refresh
- Claiming the embedded server starts after run() returns
- Thinking you must pass every @Configuration class, not just the primary source