Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Spring Boot environment variables: bind the intended key, not a lookalike

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

Spring Boot can bind an operating-system environment variable to a canonical configuration property after normalizing its name.

Download Spring source kit

Translate the key deliberately

For the relay setting relay.batch-size, replace the dot with an underscore, remove the dash, then use uppercase: RELAY_BATCHSIZE. RELAY_BATCH_SIZE looks natural, but it is a different spelling. Put the canonical property and its deployment spelling in the same change review. The typed settings bean should reject an unusable number after binding.

An environment variable usually outranks a packaged application file. It does not outrank every possible source: a command-line argument can still replace it. The earlier H2 collision shows why a programmatic default is especially weak. Record the final non-secret value and its source when diagnosing a rollout; do not print credential values.

Test the process that receives it

A test that inserts MapPropertySource into an Environment has not proved the operating system passed RELAY_BATCHSIZE to a launched process. A real boundary test must start a child process with that variable, inspect the bound value and stop it. RelaySettingsProcessTest now runs that child-process check and asserts batch 29 and timeout 750 milliseconds.

A typo may leave the packaged default active. If that default admits too many rows per tick, the worker budget changes without an obvious startup failure. For required deployment keys, validate both presence and range. Keep separate tests for CLI overrides, because the operator may intentionally supersede the environment.

Working sketch

bash
RELAY_BATCHSIZE=29
# Canonical property: relay.batch-size
# Verify the value in a launched child process before release.

Verification boundary

RelaySettingsProcessTest.actualEnvironmentBindsTheCanonicalRelayKeys checks inherited environment values in a child JVM.

Costs and limits

The mapping is a documented configuration rule, not a checked deployment environment in this kit. Shells, container manifests and secret injection still need an end-to-end test.

Common Mistakes

  • Do not invent an extra underscore for a dash in a canonical key.
  • Do not treat a manual property source as a process-environment test.
  • Do not log a secret to explain property precedence.

Read next

Spring Boot configuration properties: bind values and reject bad startup input, Spring Boot configuration sources: test each deployment path, not a guessed order, Spring Boot config source priority: a builder default may lose to a packaged file, Spring outbox polling: cap work per tick and measure backlog age.

Checked process continuation

Continue with Spring Boot environment binding: prove the value in a child JVM.

spring
spring-boot
release
Storage details