π 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:
ApplicationContextInitializersApplicationListeners
These hooks allow developers and frameworks to customize behavior before and during context startup.
πΉ Environment Preparation
Spring Boot prepares the Environment, which includes:
application.propertiesorapplication.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 applicationsAnnotationConfigReactiveWebServerApplicationContextβ for reactive appsAnnotationConfigApplicationContextβ 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-webis present β Spring configures:DispatcherServlet- Embedded Tomcat
- Default error handling
- If Spring Data JPA is present β Spring configures:
EntityManagerFactoryDataSource- 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:
@PostConstructmethods are executedBeanPostProcessors 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:
HandlerMappingsHandlerAdaptersViewResolvers
This setup enables smooth request routing from URLs to controller methods.
9οΈβ£ Application Ready State
Finally, Spring publishes key lifecycle events:
ApplicationStartedEventApplicationReadyEvent
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.