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)
flowchart LR
subgraph sources["Property sources, highest priority first"]
direction TB
t["test properties"] --> cli["--command-line args"] --> json["SPRING_APPLICATION_JSON"] --> sys["-D system properties"] --> env["OS environment variables"] --> prof["application-{profile}.yml<br/>(outside jar, then inside)"] --> app["application.yml<br/>(outside jar, then inside)"] --> ps["@PropertySource"] --> def["default properties"]
end
sources --> e["«Environment»"]
e --> b["Binder<br/>relaxed binding, type conversion"]
b --> p["BoxProperties<br/>«@ConfigurationProperties(prefix = box)»"]
b -. "@Validated" .-> v{"Bean Validation"}
v -- invalid --> fail["startup fails"]
p --> s["BoxInfoService (injected)"]Key pieces
application.properties/application.ymlinclasspath:/,classpath:/config/,./and./config/application-{profile}.yml+spring.profiles.active; YAML documents withspring.config.activate.on-profile@ConfigurationProperties(prefix = "box")on a class or a record@ConfigurationPropertiesScanor@EnableConfigurationProperties(BoxProperties.class)@Validated+ Bean Validation;@DefaultValuefor defaults in constructorsspring.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_athletesand the variableBOX_MAXATHLETESreach the same field; the canonical form is kebab-case @ConfigurationPropertiesconverts 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
@ConfigurationPropertiesclass isn't a bean by itself: you need@ConfigurationPropertiesScan,@EnableConfigurationPropertiesor@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.propertiesandapplication.yml, the.propertiesone wins @ConfigurationPropertiesdoesn't support SpEL;@Valuedoes