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-Configuration โ†“ Start Embedded Server โ†“ Initialize Beans โ†“ Run ApplicationRunner / CommandLineRunner โ†“ App is READY to Handle Requests

Happy Learning :)

No comments:

Post a Comment