Skip to content
Ameen Abozaid
JNR-012

Configuration management

  • 5 Lessons
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • application.yml
  • profiles
  • @ConfigurationProperties
  • validation

Learn first

2 h of practice (estimate)

Learning outcomes

  1. Configure the application with application.yml and profile-specific files.
  2. Bind configuration to typed, immutable @ConfigurationProperties records.
  3. Validate configuration at startup so the app fails fast on bad values.
  4. Explain Spring's property-source precedence.

Lessons

  1. 01application.yml and profiles
  2. 02Typed configuration with @ConfigurationProperties
  3. 03Validating configuration
  4. 04Property-source precedence
  5. 05Demo: typed, validated ShopProperties that fail fast

Lab and exercises

Lab

JNR-012-LAB1 · Shop settings

Outcomes: 1–4 · Time: ~2 h

  1. Create a ShopProperties record: currency, tax rate, page-size limits (default and max).
  2. Validate it with @Validated and constraints.
  3. Add application.yml plus application-dev.yml and application-prod.yml with different values.
  4. Inject ShopProperties into a service; no @Value in the code.
  5. 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)

IDOutcomeTask
JNR-012-EX011Add a dev profile that changes the log level and the port; start with it.
JNR-012-EX022Convert three @Value usages to one @ConfigurationProperties record.
JNR-012-EX032Map page-size-max in YAML to a Java record component; state the name rule.
JNR-012-EX043Add @Min(1) to the page size and set it to 0; read the failure message.
JNR-012-EX054Set the same property in application.yml, a profile file, an env var, and a command-line argument; predict, then check the winner.
JNR-012-EX064Decide where each lives: database password, tax rate, page-size limit, API token.
Show solutions
  • EX01 application-dev.yml with server.port: 8081 and logging.level.com.shop: DEBUG; start with --spring.profiles.active=dev.
  • EX02 @ConfigurationProperties(prefix="shop") record ShopProperties(...); inject it where @Value fields 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-max and 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.

  1. 1Which file adds settings only for the dev profile?O1
  2. 2Choose a profile without editing files:O1
  3. 3Annotation to bind a group of settings to a type:O2
  4. 4Why a record for properties?O2
  5. 5page-size-max in YAML binds to the Java name:O2
  6. 6What does @Validated on a properties record enable?O3
  7. 7A property fails validation. The app:O3
  8. 8Which source wins?O4
  9. 9Highest priority among these:O4
  10. 10Where do database passwords belong?O4

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).

Type to search levels, phases, modules and lessons.