πŸš€ 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

Navya S

Java developer and blogger. Passionate about clean code, JVM internals, and sharing knowledge with the community.

Leave a Reply

Your email address will not be published. Required fields are marked *