Cheatsheet

Boot properties

Key idea

Spring Boot loads properties from many sources (`application.properties`/`.yml` files, profile-specific files, environment variables, system properties, arguments…) in a fixed precedence order, and `@ConfigurationProperties` pours them into an object with types, defaults and validation.

Diagram (UML)

ApplicationContextapplication.ymlBinderapplication-prod.ymlEnvironmentBoxProperties30BoxInfoServiceBean Validation--args · -D

Key pieces

  • application.properties / application.yml in classpath:/, classpath:/config/, ./ and ./config/
  • application-{profile}.yml + spring.profiles.active; YAML documents with spring.config.activate.on-profile
  • @ConfigurationProperties(prefix = "box") on a class or a record
  • @ConfigurationPropertiesScan or @EnableConfigurationProperties(BoxProperties.class)
  • @Validated + Bean Validation; @DefaultValue for defaults in constructors
  • spring.config.import, spring-boot-configuration-processor (metadata for the IDE)

BoxProperties.java

@ConfigurationProperties(prefix = "box")   // binds box.* onto this record, by name
@Validated                                  // Bean Validation at startup
public record BoxProperties(
    @NotBlank String name,
    @Min(1) @Max(50) int maxAthletes,               // box.max-athletes, box.maxAthletes, BOX_MAXATHLETES…
    @DefaultValue("60m") Duration classDuration,    // "60m" → Duration.ofMinutes(60)
    List<String> plans,                             // YAML list or "a,b,c"
    @Valid Address address) {                       // nested object: box.address.*

    public record Address(String street, @NotBlank String city) {
    }
}

Remember for the exam

  • Precedence (highest to lowest, summarised): test properties → command-line arguments → SPRING_APPLICATION_JSON → system properties (-D) → environment variables → config files → @PropertySource → default properties
  • Among files: profile-specific ones beat general ones, and those outside the jar beat those inside
  • Relaxed binding: box.max-athletes, box.maxAthletes, box.max_athletes and the variable BOX_MAXATHLETES reach the same field; the canonical form is kebab-case
  • @ConfigurationProperties converts types: Duration (60m, PT45M), DataSize, lists, maps and nested objects
  • A record or a single constructor binds through the constructor (immutable); a class with setters, through setters
  • With @Validated, an invalid value prevents the application from starting

Exam traps

  • A @ConfigurationProperties class isn't a bean by itself: you need @ConfigurationPropertiesScan, @EnableConfigurationProperties or @Component
  • Relaxed binding doesn't apply to @Value, which needs the exact name (in its canonical kebab-case form)
  • If the same directory has application.properties and application.yml, the .properties one wins
  • @ConfigurationProperties doesn't support SpEL; @Value does

← Boot properties · Run it