Configuration management
On this page 8 topics
Overview
Topics
- application.yml
- profiles
- @ConfigurationProperties
- validation
Learn first
2 h of practice (estimate)
Learning outcomes
- Configure the application with
application.ymland profile-specific files. - Bind configuration to typed, immutable
@ConfigurationPropertiesrecords. - Validate configuration at startup so the app fails fast on bad values.
- Explain Spring's property-source precedence.
Lessons
Lab and exercises
Lab
JNR-012-LAB1 · Shop settings
Outcomes: 1–4 · Time: ~2 h
- Create a
ShopPropertiesrecord: currency, tax rate, page-size limits (default and max). - Validate it with
@Validatedand constraints. - Add
application.ymlplusapplication-dev.ymlandapplication-prod.ymlwith different values. - Inject
ShopPropertiesinto a service; no@Valuein the code. - Prove precedence: override one value with an environment variable and a command-line argument.
Acceptance: invalid config prevents startup with a clear message; profiles switch values; no secrets in committed YAML.
Exercises (assessment: lab check plus precedence quiz)
| ID | Outcome | Task |
|---|---|---|
| JNR-012-EX01 | 1 | Add a dev profile that changes the log level and the port; start with it. |
| JNR-012-EX02 | 2 | Convert three @Value usages to one @ConfigurationProperties record. |
| JNR-012-EX03 | 2 | Map page-size-max in YAML to a Java record component; state the name rule. |
| JNR-012-EX04 | 3 | Add @Min(1) to the page size and set it to 0; read the failure message. |
| JNR-012-EX05 | 4 | Set the same property in application.yml, a profile file, an env var, and a command-line argument; predict, then check the winner. |
| JNR-012-EX06 | 4 | Decide where each lives: database password, tax rate, page-size limit, API token. |
Show solutions
- EX01
application-dev.ymlwithserver.port: 8081andlogging.level.com.shop: DEBUG; start with--spring.profiles.active=dev. - EX02
@ConfigurationProperties(prefix="shop") record ShopProperties(...); inject it where@Valuefields were. - EX03
pageSizeMax; kebab-case or camelCase in the file, relaxed binding matches it. - EX04 Startup fails with a binding or validation error naming
shop.page-size-maxand the constraint. - EX05 Command line wins, then the environment variable, then the profile file, then the base file.
- EX06 Password and token: environment or secret store. Tax rate and page size: committed YAML (non-secret) with profile overrides.
Quiz
Outcomes: O1 YAML and profiles · O2 typed properties · O3 validation · O4 precedence.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises JNR-012-EX01 to EX06 plus the lab (with the precedence check in step 5). Pass: quiz at least 8/10, lab acceptance met, EX05 predicted correctly.
Common mistakes
@Value scattered everywhere; secrets in application.yml.
Assessment
Lab check plus precedence quiz.
Career relevance
Deployment configuration (JNR-052).