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:
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
Post Comment