Deep Dive Into SpringApplication.run() Mechanism
Explore the internal mechanics behind SpringApplication.run() in Spring Boot, from application type deduction to traffic readiness.

Stock photo for illustration only, not from the actual event
- Spring Boot applications can be launched using either static methods or manual instantiation.
- Internal processes deduce WebApplicationType and load initializers via SpringFactoriesLoader.
- The lifecycle includes environment preparation, banner printing, and ApplicationContext creation.
- Final execution involves ApplicationRunner and marking the app ready to accept traffic.
Every Spring Boot application starts in a remarkably consistent manner, offering developers two distinct approaches to launch their software depending on whether custom configurations are required prior to runtime execution. The primary approach relies on a static method, which accounts for roughly 95% of standard use cases, while manual instantiation is reserved for advanced customization needs.
Utilizing manual instantiation via SpringApplicationBuilder proves especially valuable when developers need to establish a parent-child context hierarchy. This particular architectural pattern is frequently leveraged within Spring Cloud for bootstrap contexts and various testing scenarios, granting developers precise control over context propagation and configuration boundaries.
Once the execution sequence is triggered, the framework performs several critical steps under the hood. This includes deducing the application type as WebApplicationType and loading ApplicationContextInitializers alongside ApplicationListeners through the SpringFactoriesLoader mechanism, which reads configuration entries directly from META-INF/spring.factories files.

Stock photo for illustration only, not from the actual event
To handle early-stage object management, a BootstrapContext acts as a dedicated container for objects required before the main ApplicationContext comes into existence. It persists from the beginning of the run method until the context is fully prepared, facilitating the registration of early beans such as the EnvironmentPostProcessor.
The internal architecture of Spring Boot is meticulously structured around modular extension points. Gaining insight into the step-by-step lifecycle from environment preparation to runner execution empowers developers to hook into critical phases, optimize startup behavior, and manage enterprise application constraints effectively.
Subsequent phases in the Spring Boot 3.x execution skeleton involve preparing the environment, printing the startup banner, creating the appropriate application context type, and refreshing the container to initialize the core Spring bean factory.
Toward the final stages of the startup sequence, the application publishes the ApplicationStartedEvent, indicating that the core process is running although not yet accepting external requests. Full operational readiness is achieved once ApplicationRunners execute and the ApplicationReadyEvent alongside ReadinessState changes are broadcasted to traffic management components.
Source: Dev.to
Found something wrong in this article? Report an issue with this article
Comments
Leave a Comment