Sobes.tech
Middle+

What happens from the moment a Spring application starts until the first request in Postman?

sobes.tech AI

Answer from AI

  1. Configuration loading: The JVM runs the main method. Spring Boot automatically detects the class with the main method and starts the auto-configuration process. It looks for the @SpringBootApplication annotation (or its composition).

  2. Creating the application context: Spring creates an ApplicationContext. This is the central interface that provides configuration for the application. The context manages the lifecycle of beans.

  3. Component scanning: Spring scans the specified packages (by default — the package containing the main application class) for annotations indicating Spring components (@Component, @Service, @Repository, @Controller, and their specializations).

  4. Creating and injecting beans: For found components, Spring creates instances (beans) and manages their dependencies using dependency injection mechanisms via @Autowired, @Resource, constructors, or setters.

  5. Auto-configuration: Based on classpath, application properties, and other factors, Spring Boot automatically configures various components such as data sources, web servers (e.g., Tomcat by default), Spring MVC, JPA, etc. For example, if there is a spring-boot-starter-web dependency, Spring Boot automatically configures an embedded web server and Spring MVC.

  6. Starting the embedded web server: If the application is web-type, Spring Boot starts an integrated web server (Tomcat, Undertow, or Jetty) that listens for incoming HTTP requests on a specified port (default 8080).

  7. Initializing Spring MVC (for web applications):

    • A DispatcherServlet is created — the central servlet of Spring MVC, which handles all incoming web requests.
    • The DispatcherServlet initializes the Spring MVC infrastructure: HandlerMapping (mapping URLs to handlers), HandlerAdapter (calling handlers), ViewResolver (resolving view names), etc.
    • Classes annotated with @Controller or @RestController are scanned, and methods annotated with request mappings (@GetMapping, @PostMapping, etc.) are registered in HandlerMapping.
  8. Application is ready: After completing all these steps, the application is fully initialized, and the web server is ready to accept incoming HTTP requests.

When the first request arrives from Postman:

  1. Request reception: The embedded web server accepts the HTTP request.
  2. Passing to DispatcherServlet: The request is passed to DispatcherServlet.
  3. Handler determination: DispatcherServlet uses HandlerMapping to determine the controller and method corresponding to the URL and HTTP method of the incoming request.
  4. Handler invocation: HandlerAdapter calls the found controller method, passing request parameters (e.g., path variables, query parameters, request body).
  5. Request processing: The controller method executes business logic, interacts with services and repositories as needed.
  6. Response formation: The controller returns a result (e.g., a data object or view name). For @RestController, the result is automatically serialized (e.g., into JSON).
  7. Sending the response: DispatcherServlet or the appropriate result handler sends the HTTP response back to the client (Postman).