Saturday, 5 April 2025

 

🚀 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:


@SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } }

Here’s what kicks off the magic.


⚙️ 2. Spring Boot Creates a SpringApplication Instance

Internally, Spring Boot creates an instance of SpringApplication.


SpringApplication app = new SpringApplication(MyApp.class);

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 or application.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:


META-INF/spring.factories

Example:


EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

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:


@Component public class MyRunner implements CommandLineRunner { public void run(String... args) { System.out.println("App started!"); } }

Spring Boot will run it after startup.


🖥️ 9. Application Is Up and Ready!

You’ll see:


Started MyApp in 3.456 seconds Tomcat started on port(s): 8080 (http)

🎉 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


@RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello, Spring Boot!"; } }

🧠 Recap: Spring Boot Startup Flow


@SpringBootApplication ↓ SpringApplication.run() ↓ Prepare Environment & Context ↓ Load application.properties ↓ Component Scanning ↓ Auto-ConfigurationStart Embedded Server ↓ Initialize Beans ↓ Run ApplicationRunner / CommandLineRunner ↓ App is READY to Handle Requests

Happy Learning :)

No comments:

Post a Comment