🚀 What Happens When You Start a Spring Boot Project – Step-by-Step
Have you ever wondered what actually happens under the hood when you run a Spring Boot application? It looks simple on the surface, but a lot is going on behind the scenes.
In this post, I’ll walk you through each step in the Spring Boot startup process—from hitting "Run" to handling web requests.
🟢 1. You Hit Run
or Execute SpringApplication.run()
Your entry point looks like this:
Here’s what kicks off the magic.
⚙️ 2. Spring Boot Creates a SpringApplication
Instance
Internally, Spring Boot creates an instance of SpringApplication
.
It also sets up:
-
Environment
-
ApplicationContext
-
Banner
-
Listeners
-
Initializers
🏗️ 3. Environment Is Prepared
Spring Boot prepares the environment by:
-
Loading system properties
-
Reading
application.properties
orapplication.yml
-
Setting up profiles (like
dev
,prod
, etc.) -
Merging environment variables
This is handled by ConfigFileApplicationListener
.
🔍 4. ApplicationContext Is Created
Depending on the project type, one of the following is created:
-
AnnotationConfigApplicationContext
(non-web) -
AnnotationConfigServletWebServerApplicationContext
(web apps)
🔎 5. Component Scanning & Bean Definition
Spring Boot uses:
-
@ComponentScan
to find@Component
,@Service
,@Repository
, etc. -
@Configuration
to load custom configs
These classes are registered as Spring Beans in the ApplicationContext
.
🔧 6. Auto-Configuration via @EnableAutoConfiguration
Spring Boot checks all entries in:
Example:
It loads only the relevant configurations based on classpath dependencies.
🧱 7. Embedded Server (like Tomcat) Starts
If it’s a web project, Spring Boot:
-
Auto-configures Tomcat (or Jetty, Undertow)
-
Registers DispatcherServlet
-
Binds server port (default
8080
) -
Starts the HTTP listener
No need to deploy a WAR file — it runs as a standalone JAR!
🔄 8. ApplicationRunner or CommandLineRunner Executes
If your project has logic like this:
Spring Boot will run it after startup.
🖥️ 9. Application Is Up and Ready!
You’ll see:
🎉 Your app is now live at http://localhost:8080
🌐 10. Request Handling Begins
From now on:
-
DispatcherServlet
routes incoming HTTP requests -
Controllers like
@RestController
respond to them
🧠 Recap: Spring Boot Startup Flow
No comments:
Post a Comment