The Internal Working of Spring Boot

The internal working of Spring Boot

Spring Boot is designed to simplify the creation of production-grade Spring applications by providing a set of conventions and defaults. Here’s how Spring Boot works internally:

1. SpringApplication Class:

  • Role: The `SpringApplication` class is the entry point of any Spring Boot application. It sets up the default configuration and starts the Spring application context.
  • How It Works: When the `run` method is called, it performs a series of steps to bootstrap the application, including setting up the environment, creating the application context, and starting the embedded server.

2. Auto-Configuration:

  •  Role: Spring Boot’s auto-configuration feature automatically configures the application based on the dependencies present in the classpath.
  •  How It Works: It uses `@EnableAutoConfiguration` and various conditional annotations to configure beans automatically. For example, if `spring-boot-starter-web` is present, it configures a `DispatcherServlet` and other necessary web components.

3. SpringApplication.run():

  •  Role: This method is the starting point for a Spring Boot application. It triggers the auto-configuration process and starts the application.
  •  How It Works: It initializes the Spring context, applies auto-configuration, and starts the embedded server (if any).

4. Embedded Server:

  • Role: Spring Boot can run web applications using embedded servers such as Tomcat, Jetty, or Undertow.
  • How It Works: If `spring-boot-starter-web` is included, Spring Boot configures an embedded server and starts it along with the application.

5. Actuator

  • Role: Provides production-ready features like health checks, metrics, and monitoring.
  • How It Works: By including `spring-boot-starter-actuator`, various endpoints are made available to monitor and manage the application.

Internal Working of Spring Boot

SpringApplication Class
MainApplication.java
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

In this example:

  • SpringApplication.run: Initializes the Spring application context and starts the embedded server.

Auto-Configuration

application.properties
```properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
```

In this example:

  • Auto-Configuration: Automatically configures the server port and data source based on the properties file.

Embedded Server

MainApplication.java
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

In this example:

Embedded Server: Configures and starts an embedded server (Tomcat by default).

Actuator
pom.xml
```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
```

application.properties
```properties
management.endpoints.web.exposure.include=health,info
```

HealthController.java
```java
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HealthController {

    @GetMapping("/health")
    public String health() {
        return "Application is up and running";
    }
}
```

In this example:

  • Actuator: Provides health and info endpoints for monitoring the application.

Summary

  • SpringApplication: Initializes and runs the Spring application.
  • Auto-Configuration: Automatically configures beans based on classpath settings.
  • Embedded Server: Allows running applications with embedded web servers.
  • Actuator: Provides monitoring and management endpoints.

Homepage

Readmore