On this page
  1. Spring Boot
  2. Overview
  3. Core Building Blocks
  4. Minimal REST Service + Typed Config (Java 21)
  5. Virtual Threads on Spring Boot 3.2+ (Java 21)
  6. Testing Slices vs. Full Context

Spring Boot

Prev: Spring Framework | Up: third_party/java | Next: interop
Rel: Spring Framework, Jackson, Apache Maven, Gradle, JUnit 5, record
Ref: Spring Boot Reference Documentation
Tags: #public #prog #java #third-party #framework #web #tier/core


Overview

Spring Boot builds on top of Spring Framework to eliminate boilerplate XML/Java setup. It provides opinionated starter dependencies, auto-configuration driven by what is on the classpath, an embedded servlet/reactive server (Tomcat, Jetty, or Undertow) packaged into a runnable fat JAR, and production observability via Actuator.

Core Building Blocks

Feature How It Works
Starters Curated POMs like spring-boot-starter-web, spring-boot-starter-data-jpa, spring-boot-starter-test that align transitive versions
Auto-Configuration @SpringBootApplication scans components and conditionally registers beans (@ConditionalOnClass, @ConditionalOnMissingBean)
Externalized Config application.yml / application.properties, environment variables, and profiles (spring.profiles.active=prod) bound to typed @ConfigurationProperties records
Embedded Server Runs directly via java -jar app.jar—no external WAR deployment container needed
Actuator Adds /actuator/health, /actuator/metrics, and /actuator/info endpoints for container liveness/readiness probes

Minimal REST Service + Typed Config (Java 21)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

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

// Bound automatically from app.greeting in application.yml
@ConfigurationProperties(prefix = "app")
record AppProperties(String greeting) {}

@RestController
class HelloController {
    private final AppProperties props;

    HelloController(AppProperties props) {
        this.props = props;
    }

    @GetMapping("/hello/{name}")
    MessageResponse hello(@PathVariable String name) {
        return new MessageResponse(props.greeting() + ", " + name);
    }

    record MessageResponse(String message) {}
}

Virtual Threads on Spring Boot 3.2+ (Java 21)

Enable virtual threads for Tomcat/Jetty request handling with a single property in application.yml:

spring:
  threads:
    virtual:
      enabled: true

Testing Slices vs. Full Context

Linked from

Related notes