Saturday, 5 April 2025

 

How Spring Boot Works Internally – Step-by-Step with Real Example

Spring Boot makes it easy to create stand-alone, production-grade Spring applications with minimal configuration. But what really happens when you run a Spring Boot app?

Let’s break it down step-by-step with code and an easy-to-understand flow.


1. Application Entry Point – @SpringBootApplication


@SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } }
  • @SpringBootApplication is a meta-annotation that includes:

    • @Configuration – Marks this class as a source of bean definitions

    • @EnableAutoConfiguration – Tells Spring Boot to auto-configure beans

    • @ComponentScan – Enables scanning for components (@Component, @Service, etc.)


2. Bootstrapping with SpringApplication.run()

When SpringApplication.run(MyApp.class, args) is called:

  • It creates a SpringApplication instance

  • Prepares the environment and application context

  • Loads application.properties or application.yml

  • Registers listeners and initializers


3. Auto-Configuration Magic

Spring Boot looks into the META-INF/spring.factories file and loads @Configuration classes based on dependencies.

Example:


org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  • If spring-web is present → WebMvcAutoConfiguration kicks in

  • If spring-boot-starter-data-jpa is present → JPA configurations are loaded


4. Embedded Server Starts Automatically

If it's a web app:

  • Tomcat is auto-configured and started by Spring Boot

  • No need for external WAR deployment


TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();

App runs on http://localhost:8080 by default


5. Bean Scanning and Registration

Spring Boot scans for annotations like:

  • @Component

  • @Service

  • @Repository

  • @RestController

And registers them as beans in the ApplicationContext.


6. Request Dispatching with DispatcherServlet

Spring Boot registers DispatcherServlet automatically for web apps.

Example Controller:


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

Request to /hello is handled by this method.


7. Application Ready

Once the context is loaded, Spring Boot triggers:

  • CommandLineRunner or ApplicationRunner if present

  • Prints a log like:

    Started MyApp in 3.456 seconds

Quick Example: Complete App


@SpringBootApplication public class BlogDemoApplication { public static void main(String[] args) { SpringApplication.run(BlogDemoApplication.class, args); } } @RestController class DemoController { @GetMapping("/") public String home() { return "Welcome to my blog!"; } }

Run it, and your app is live at http://localhost:8080/


Spring Boot Internals – Summary Flow


@SpringBootApplication ↓ SpringApplication.run() ↓ Load Properties & Beans ↓ Auto-Configuration via spring.factories ↓ Embedded Tomcat/Jetty Starts ↓ Component Scanning & Bean Registration ↓ DispatcherServlet Routes Requests ↓ App is Ready to Serve




No comments:

Post a Comment