Skip to content
Ameen Abozaid
JNR-007

Configuration

  • 6 Lessons
  • 66 min of reading
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Properties
  • YAML
  • profiles
  • environment variables
  • externalized configuration

Learn first

66 min of reading · 2 h of practice (estimate)

Learning outcomes

  1. Externalize configuration with properties files, YAML, and environment variables.
  2. Apply the twelve-factor principle of separating config from code.
  3. Explain configuration precedence and per-environment profiles.
  4. Keep secrets out of source control.

Lessons

  1. 01Why externalize configuration11 min read
  2. 02Properties and YAML11 min read
  3. 03Environment variables and system properties11 min read
  4. 04Profiles and precedence11 min read
  5. 05Secrets do not belong in Git11 min read
  6. 06Demo: one JAR, three environments11 min read

Lab and exercises

Lab

JNR-007-LAB1 · Configurable CLI

Outcomes: 1–4 · Time: ~1.5 h Read currency, tax rate, and an API key from a file in the Shop CLI. Allow each to be overridden by an environment variable. Commit a .env.example instead of real values and add .env to .gitignore. Acceptance: the same JAR behaves differently per environment; no secrets in Git; missing required values give a clear error; the README states the precedence order.

Exercises (assessment: quiz on precedence plus lab check; six exercises)

IDOutcomeTask
JNR-007-EX011Move three hard-coded values (currency, tax rate, shipping fee) into application.properties and load them.
JNR-007-EX021Write the same settings in YAML, and mark the indentation rule.
JNR-007-EX031Override one value with an environment variable and another with -D. Show both commands.
JNR-007-EX042Identify the twelve-factor violation: if (System.getenv("ENV").equals("prod")) fee = 5;. Fix it.
JNR-007-EX053Given file=0.19, profile file=0.16, env var=0.07, command line absent, which value wins? Write the order.
JNR-007-EX064A teammate committed .env with an API key. List the steps to fix it.
Show solutions
  • EX01 Properties p = new Properties(); p.load(in); and p.getProperty("shop.currency").
  • EX02 shop: then two-space indented keys; no tabs.
  • EX03 SHOP_TAX_RATE=0.07 java -jar shop-cli.jar and java -Dshop.currency=USD -jar shop-cli.jar.
  • EX04 Environment-specific code. Move the fee into configuration (shop.shipping-fee) and set it per environment.
  • EX05 0.07 (environment variable). Order: file < profile file < environment variable < command line.
  • EX06 Revoke and replace the key; git rm --cached .env; add .env to .gitignore; commit .env.example; clean history if the policy needs it.
  • Outcome 3 is also covered by quiz Q06–Q07; outcome 2 by EX04 and quiz Q02.

Quiz

Outcomes: O1 externalize config · O2 twelve-factor · O3 precedence and profiles · O4 secrets.

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. 1Configuration is best kept:O1
  2. 2The twelve-factor rule for config says:O2
  3. 3Which reads an environment variable?O1
  4. 4How do you set a system property?O1
  5. 5In YAML, nesting is shown by:O1
  6. 6File says 0.19, environment variable says 0.07. The usual winner is:O3
  7. 7A profile is:O3
  8. 8Where should a real API key live?O4
  9. 9What do you commit for local setup?O4
  10. 10A key was committed by mistake. First, you:O4

Practical part

Six exercises JNR-007-EX01 to EX06 plus the lab check (same JAR, different behavior). Pass: at least 8 of 10 quiz answers including the precedence question, five exercises correct, and no secrets in the repository.

Common mistakes

Committing .env; environment-specific if statements in code.

Assessment

Quiz on precedence plus lab check.

Career relevance

Spring configuration (JNR-012) and secure configuration (JNR-042).

Type to search levels, phases, modules and lessons.