What Happens When a Spring Boot App Starts? ๐Ÿš€

๐Ÿš€ What Happens When You Run a Spring Boot Application? (High-Level Theory)

Spring Boot is designed to remove the heavy lifting involved in configuring and running Java applications. As a developer, you often just write a main() method, annotate your class with @SpringBootApplication, and run the app. Behind the scenes, however, a lot of intelligent orchestration happens inside the JVM.

Understanding this internal flow is extremely useful for interviews, debugging, performance tuning, and real-world system design. Letโ€™s break down what actually happens when you run a Spring Boot application, step by step, at a high theoretical level.


1๏ธโƒฃ JVM Starts Execution

Everything begins with the Java Virtual Machine (JVM).

When you run your Spring Boot application:

  • The JVM is started by the operating system
  • The JVM loads the main class into memory
  • The public static void main(String[] args) method is invoked

Inside this main() method, Spring Boot calls:

SpringApplication.run(Application.class, args);

This single line is the entry point to the entire Spring Boot startup process.


2๏ธโƒฃ SpringApplication Bootstraps the Application

SpringApplication is the core class responsible for bootstrapping the application. At this stage, Spring Boot performs several critical tasks:

๐Ÿ”น Application Type Detection

Spring Boot automatically determines whether your application is:

  • Servlet-based web application (Spring MVC)
  • Reactive web application (Spring WebFlux)
  • Non-web application

This decision is based on classpath dependencies (for example, whether spring-webmvc or spring-webflux is present).

๐Ÿ”น Loading Initializers and Listeners

Spring loads:

  • ApplicationContextInitializers
  • ApplicationListeners

These hooks allow developers and frameworks to customize behavior before and during context startup.

๐Ÿ”น Environment Preparation

Spring Boot prepares the Environment, which includes:

  • application.properties or application.yml
  • Active profiles (dev, test, prod)
  • System properties
  • Environment variables

At this stage, configuration values become available to the application.


3๏ธโƒฃ ApplicationContext Creation

Next, Spring creates the ApplicationContext, which is the heart of the Spring Framework.

Depending on the application type, Spring Boot chooses the appropriate context:

  • AnnotationConfigServletWebServerApplicationContext โ†’ for web applications
  • AnnotationConfigReactiveWebServerApplicationContext โ†’ for reactive apps
  • AnnotationConfigApplicationContext โ†’ for non-web apps

The ApplicationContext:

  • Acts as a container for beans
  • Manages bean lifecycle
  • Handles dependency injection
  • Publishes application events

Without this context, Spring as a framework cannot function.


4๏ธโƒฃ Auto-Configuration Kicks In

This is where Spring Boot truly shines.

๐Ÿ”น How Auto-Configuration Works

Spring Boot scans the classpath to understand which libraries are present and automatically configures beans accordingly. This is enabled by @EnableAutoConfiguration, which is part of @SpringBootApplication.

๐Ÿ”น Examples

  • If spring-boot-starter-web is present โ†’ Spring configures:
    • DispatcherServlet
    • Embedded Tomcat
    • Default error handling
  • If Spring Data JPA is present โ†’ Spring configures:
    • EntityManagerFactory
    • DataSource
    • Transaction management
  • If no explicit configuration is provided โ†’ Spring applies sensible defaults

This mechanism drastically reduces boilerplate and configuration effort.


5๏ธโƒฃ Component Scanning

Spring Boot now scans your application packages to find Spring-managed components.

The base package is determined by the location of the class annotated with @SpringBootApplication.

๐Ÿ”น What Gets Detected?

Classes annotated with:

  • @Component
  • @Service
  • @Repository
  • @Controller
  • @RestController
  • @Configuration

All detected classes are registered as beans inside the ApplicationContext.


6๏ธโƒฃ Bean Creation & Dependency Injection

Once all bean definitions are known, Spring begins creating beans.

๐Ÿ”น Bean Instantiation

  • Beans are instantiated based on scope (singleton, prototype, etc.)
  • Constructor-based injection is preferred
  • Field and setter injection are also supported

๐Ÿ”น Dependency Injection

Spring resolves dependencies using:

  • Constructor injection
  • @Autowired
  • @Qualifier
  • @Value

๐Ÿ”น Bean Lifecycle

During this phase:

  • @PostConstruct methods are executed
  • BeanPostProcessors are applied
  • Initialization callbacks are triggered

This ensures all beans are fully wired and ready to use.


7๏ธโƒฃ Embedded Server Starts

Spring Boot uses embedded servers, which eliminates the need for external application servers.

Depending on dependencies, Spring Boot starts:

  • Tomcat (default)
  • Jetty
  • Netty

The server:

  • Binds to the configured port (default: 8080)
  • Initializes network connectors
  • Prepares to accept HTTP requests

At this point, your application is officially running as a web server.


8๏ธโƒฃ DispatcherServlet Initialization

The DispatcherServlet is the front controller of Spring MVC.

๐Ÿ”น Responsibilities

  • Receives incoming HTTP requests
  • Delegates requests to appropriate controllers
  • Handles request mapping, validation, and response rendering

Spring initializes:

  • HandlerMappings
  • HandlerAdapters
  • ViewResolvers

This setup enables smooth request routing from URLs to controller methods.


9๏ธโƒฃ Application Ready State

Finally, Spring publishes key lifecycle events:

  • ApplicationStartedEvent
  • ApplicationReadyEvent

Once ApplicationReadyEvent is published:

  • All beans are fully initialized
  • Server is up and running
  • Application is ready to serve requests ๐Ÿš€

You can hook into this phase using:

@EventListener(ApplicationReadyEvent.class)

๐ŸŽฏ One-Line Interview Answer

When a Spring Boot application runs, it starts the JVM, bootstraps SpringApplication, creates the application context, performs auto-configuration, scans and initializes beans, starts the embedded server, initializes the DispatcherServlet, and finally becomes ready to handle HTTP requests.


๐Ÿ“Œ Final Thoughts

Spring Boot hides a complex startup lifecycle behind a simple abstraction. While developers enjoy faster development and minimal configuration, understanding this flow helps you:

  • Debug startup issues
  • Optimize application performance
  • Answer interview questions confidently
  • Design scalable Spring applications

Post Comment