Understanding Spring Boot Startup: From run() to Application Context
This article explains how Spring Boot launches an application by invoking SpringApplication.run(), initializing the SpringApplication instance, and executing the run method, detailing each internal step, configuration loading, listener handling, and context refresh with illustrative code examples.
Spring Boot Startup Overview
Spring Boot bundles the full Spring ecosystem and an embedded container, allowing developers to start a project with minimal configuration through auto‑configuration and a one‑click deployment model.
Step 1 – Launch the Application
The entry point is a class annotated with @SpringBootApplication that calls SpringApplication.run():
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
// Create a SpringApplication instance and run it
SpringApplication.run(UserApplication.class, args);
}
}Step 2 – SpringApplication Constructor
The constructor sets default properties, loads factories, determines the web application type, and prepares the main class for component scanning:
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
this.sources = new LinkedHashSet();
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = new HashSet();
this.isCustomEnvironment = false;
this.lazyInitialization = false;
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
// Determine container type (SERVLET, REACTIVE, NONE)
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// Load ApplicationContextInitializer implementations from META-INF/spring.factories
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
// Load ApplicationListener implementations from META-INF/spring.factories
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
// Resolve the main application class for component scanning
this.mainApplicationClass = this.deduceMainApplicationClass();
}Step 3 – Executing the run() Method
The run method creates a timer, prepares the environment, registers listeners, refreshes the application context, and finally starts the embedded server. It also handles custom runners, exception reporting, and logs the startup duration.
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[]{ConfigurableApplicationContext.class},
context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable ex) {
this.handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
return context;
} catch (Throwable ex) {
this.handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
}During the run process, Spring Boot loads configuration files, registers initializers and listeners, creates the appropriate ApplicationContext (Servlet, Reactive, or None), refreshes beans, and finally starts the embedded server, completing the one‑click deployment cycle.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Thoughts on Knowledge and Action
Travel together, with knowledge and action all the way
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.
