๐ 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