The Analogy: A Touring Show's Lighting Board
A touring theatre production carries its lighting design on a control board. The board holds a base scene: a stored default level for every channel -- house lights, wash, spots, the lot. When the crew loads the show cold, every fader snaps to the value written in that base scene. It is the single, complete description of how the rig should look when nothing special is going on, and it travels with the production from town to town unchanged.
When the show moves into a new venue, the lighting designer does not rebuild the whole board. The rooms differ -- a taller fly tower here, a brighter foyer there -- so they load a venue override layer on top of the base scene. That layer only touches the handful of channels the new room needs different. The board resolves each channel by reading down through the layers and taking the value from the topmost layer that mentions it; any channel the venue layer does not mention keeps whatever the base scene set. Two layers, one effective level per channel.
There is one more layer above all of these: the operator's live console. During the performance the operator can grab a single fader and push it, and that manual value beats every stored layer for as long as they hold it. So the order of authority, top to bottom, is live console, then venue layer, then base scene. A value set high in the stack always wins over the same channel set lower down, which is exactly how you ship one rig to twenty venues and adjust only what each room demands.
The board fails you in two quiet ways. If the designer bakes a value that should be venue-specific into the base scene -- say a house-light level that is only right for the home theatre -- every other venue inherits the wrong default and nobody notices until that room is dark in the wrong place. And if the venue layer labels a channel slightly wrong, the override lands on no real fader at all: the base value silently stays, the cue looks fine in the dim of rehearsal, and the mistake only shows on opening night under a full house. Both failures come from trusting the layers without checking which one actually won each channel.
Syntax or Core Structure
Spring resolves a property the same way the board resolves a channel: it reads down an ordered list of property sources and takes the value from the highest-precedence source that defines it. You bind the resolved values into a typed record with @ConfigurationProperties.
@ConfigurationProperties(prefix = "store")
@Validated
public record StoreProperties(
@NotNull Duration timeout,
@NotBlank String apiKey
) {}
The precedence order, highest first, is command-line arguments, then OS environment variables, then the active profile's application-{profile}.yml, then the base application.yml, then any default baked into the code. Trace store.timeout when the command line does not set it but every lower source does:
Output:
source (highest first) defines? value
----------------------------------------------------
command-line --store.timeout no -
OS env STORE_TIMEOUT yes PT5S
application-prod.yml yes PT10S
application.yml yes PT30S
@ConfigurationProperties def yes PT60S
----------------------------------------------------
effective value : PT5S
wins from : OS env STORE_TIMEOUT
The command line is the top layer but it says nothing about store.timeout, so resolution falls through to the OS environment variable at PT5S. The three lower sources all define the property too, but none of them is ever consulted -- the first source that sets a value wins and the rest are shadowed, precisely like a channel taken by the venue layer never reading its base value.
Technical Depth: Relaxed Binding and Profile Layers
An environment variable only overrides the name it actually matches
An environment variable does not carry dots or dashes, so Spring uses relaxed binding to decide whether a variable overrides a property. It takes the canonical property name -- camelCase folded to kebab-case, lowercased -- and generates the environment-variable form by uppercasing it and replacing every dot and dash with an underscore. The variable overrides the property only if its name equals that generated candidate exactly.
// store.max-connections and store.maxConnections are the SAME property;
// both generate the candidate STORE_MAX_CONNECTIONS.
// store.api-key generates STORE_API_KEY -- not STORE_APIKEY.
| Property name | Canonical | Env candidate |
|---|---|---|
| store.max-connections | store.max-connections | STORE_MAX_CONNECTIONS |
| store.maxConnections | store.max-connections | STORE_MAX_CONNECTIONS |
| store.api-key | store.api-key | STORE_API_KEY |
| store.timeout | store.timeout | STORE_TIMEOUT |
Output:
property name env candidate matches
----------------------------------------------------------
store.max-connections STORE_MAX_CONNECTIONS yes
store.maxConnections STORE_MAX_CONNECTIONS yes
store.api-key STORE_API_KEY no
store.timeout STORE_TIMEOUT yes
----------------------------------------------------------
bound : 3
mis-bound : 1
Three of the four provided variables match their property's candidate. The odd one out is an environment variable named STORE_APIKEY, which the model marks as a miss because store.api-key generates STORE_API_KEY with an underscore between api and key. The variable is set, spelled almost right, and silently overrides nothing.
A profile is an override layer stacked on the base
Profiles are the venue layers. The base application.yml sets every channel; application-prod.yml is loaded on top and mentions only what production needs different. Each property takes the value from the topmost layer that sets it.
# application.yml (base scene) # application-prod.yml (venue layer)
app.name: store-api db.url: jdbc:postgres:prod
db.url: jdbc:h2:mem log.level: WARN
db.pool: 5
log.level: DEBUG
Output:
channel effective from overridden
----------------------------------------------------
app.name store-api base no
db.url jdbc:postgres:prod venue yes
db.pool 5 base no
log.level WARN venue yes
----------------------------------------------------
channels : 4
from venue layer: 2
from base layer : 2
The prod layer overrides db.url and log.level and stays silent on the other two, which keep their base values. Four channels resolve to two-from-base and two-from-venue -- the same fall-through the lighting board does when a venue layer touches only the faders that room needs.
Real World Example: A Production Deploy That Ships a Placeholder Secret
Here is a configuration that passes every local test and ships a fake API key to production. The base application.yml carries a placeholder so the app boots in development, and production is meant to override it through an environment variable.
# application.yml (committed to Git)
store:
api-key: changeme # placeholder, meant to be overridden in prod
timeout: PT30S
# production deploy script
export STORE_APIKEY=sk-live-9f # typo: should be STORE_API_KEY
export STORE_TIMEOUT=PT5S
java -jar store-api.jar --spring.profiles.active=prod
Relaxed binding generates STORE_API_KEY as the candidate for store.api-key, but the deploy script exports STORE_APIKEY. The names do not match, so the environment variable overrides nothing and store.api-key falls back to the value committed in the jar. store.timeout is spelled correctly and binds. The app boots cleanly with no error, because a placeholder is still a value.
Output:
property provided env matched effective
--------------------------------------------------------
store.api-key STORE_APIKEY no changeme (committed)
store.timeout STORE_TIMEOUT yes PT5S
--------------------------------------------------------
env vars set : 2
correctly bound : 1
fell back to jar : 1
secret from jar : yes
The line that exposes the bug is correctly bound: 1 against env vars set: 2: both variables were exported, but only one landed. The other fell back to the jar, and because that jar value is a real string the application starts without complaint and runs in production authenticating with changeme. There are two design flaws here, and they compound. The first is committing a secret-shaped default at all -- api-key should have no value in application.yml, so a missing override fails the boot rather than shipping a placeholder. The second is that a single-character difference in an environment variable name silently disables an override with no warning. The honest fix is to leave store.api-key unset in the committed file and mark it @NotBlank on the properties record, so relaxed-binding failure becomes a startup failure instead of a live incident.
Common Mistakes
- Scattering
@Valueacross the codebase instead of binding a typed record. Related settings drift apart and a missing one surfaces at runtime, not startup. Group them under one@ConfigurationPropertiestype and add@Validated.
- Committing a real or placeholder secret to
application.yml. It is in Git history permanently and readable by anyone with repository access. Leave secrets unset in committed files and supply them from the environment or a secret manager.
- Assuming an environment variable overrides a property by resemblance.
STORE_APIKEYdoes not matchstore.api-key, whose candidate isSTORE_API_KEY. Generate the candidate by uppercasing the canonical name and replacing dots and dashes with underscores, then match exactly.
- Duplicating shared values across every profile file. The same value copied into dev, staging and prod drifts out of sync. Put shared settings in the base
application.ymland let each profile override only what differs.
- Expecting a lower-precedence source to win. A value in
application.ymlnever overrides the same key set by an environment variable or the command line. Learn the order and put each value at the layer that should own it.
- Forgetting to activate the profile. Without
spring.profiles.active=prodthe production overrides never load and the app runs on base defaults. Set the active profile explicitly in every non-local environment.
- Leaving no default for a value that has a sensible one. Every environment then has to set it, and a forgotten one fails to boot. Provide a safe default in the base file and override only where needed.
- Using
@Valuefor a required secret with no validation. A missing value binds to an empty string and the failure appears mid-request. Bind it to a@NotBlankfield so an absent value stops the boot.
- Logging the resolved configuration at startup. Dumping properties to the log to "verify" them leaks any secret that did bind into your log aggregator. Never log secret-bearing properties.
Best Practices
- Bind related settings into a typed
@ConfigurationPropertiesrecord and annotate it@Validatedso missing or malformed values fail at startup. - Keep secrets out of committed files entirely; leave them unset and supply them from the environment or a secret manager at deploy time.
- Mark required secrets
@NotBlankso a mis-named or missing environment override becomes a boot failure rather than a live incident. - Put shared values in the base
application.ymland let profile files override only what genuinely differs per environment. - Generate the environment-variable candidate for a property deliberately -- uppercase the canonical name, replace dots and dashes with underscores -- and match it exactly.
- Set
spring.profiles.activeexplicitly in every deployed environment rather than relying on a default profile. - Rotate any secret that has ever been committed to version control, because the history keeps it forever.
- Never log the resolved property set at startup; it is the easiest way to leak a secret that bound correctly.
Practice Exercises
- Your service needs a different database URL per environment and one API key that must never be in Git. Lay out the base and profile files, decide where each value lives, and explain which source supplies the API key in production.
- A deploy exports
DATABASE_URLbut the property isspring.datasource.urland the app still connects to the local H2 default. Work out the correct environment-variable name from the property and explain why the export was ignored.
- A teammate copies the same
store.timeoutvalue intoapplication-dev.yml,application-staging.ymlandapplication-prod.yml. Explain the maintenance risk and refactor the files so the value is defined once.
- An API key is committed to
application.ymlaschangemeso local development works, and production is meant to override it. Explain why this ships a broken secret if the override is mis-named, and rewrite the configuration so a missing override fails the boot instead.