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.

Thoughts on Knowledge and Action
Thoughts on Knowledge and Action
Thoughts on Knowledge and Action
Understanding Spring Boot Startup: From run() to Application Context

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring BootSpring Frameworkapplication startup
Thoughts on Knowledge and Action
Written by

Thoughts on Knowledge and Action

Travel together, with knowledge and action all the way

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.