Command-line properties can replace application defaults before a Spring Boot context binds its configuration objects.
Spring Boot command-line properties: test the effective value at startup
The checked value
BootRelaySettingsTest starts a non-web SpringApplication context with relay.batch-size=8 and relay.timeout=2s as defaults. It passes --relay.batch-size=12 to run. The RelaySettings bean reports 12 and keeps the two-second timeout. The assertion reads the bound object, not only the raw Environment property, so it catches an override that exists but never reaches the consumer.
This check uses SpringApplicationBuilder rather than manually inserting property sources. It proves the command-line/default ordering for this small context. It does not prove a precedence matrix involving actual operating-system environment variables, imported config files, Kubernetes secrets or remote configuration. The earlier override fixture inserts named sources directly; the matrix lesson separates those two forms of evidence.
Treat the effective setting as a contract
An operator may believe a flag was applied when a misspelled key silently leaves a default active. During startup, log a redacted subset of effective non-secret settings or expose a protected diagnostic view. Do not print credentials or access tokens. Use one canonical property prefix and document units for time and size values.
For a relay, batch size affects database load and outage recovery. Changing it without a load test can increase lock waits. The duration lesson checks that a separate timeout does not borrow an unintended unit.
Checked source
new SpringApplicationBuilder(SettingsConfig.class)
.web(WebApplicationType.NONE)
.properties("relay.batch-size=8", "relay.timeout=2s")
.run("--relay.batch-size=12");Verification boundary
BootRelaySettingsTest.commandLineOverridesPackagedDefaults. The excerpt is shortened or a labelled design sketch; the kit contains the checked tests.
Costs and limits
The test uses application defaults and one command-line argument. Separate tests now load a temporary application.properties file and check its collision with the CLI. This particular assertion does not control the host environment.
Common Mistakes
- Do not infer a deployment value from a source file alone.
- Do not log secrets while reporting effective settings.
- Do not accept an unrecognized override key without an operator check.
Read next
Spring Boot Duration binding: make timeout units explicit, Spring Boot configuration validation: reject an unusable relay before work starts, Spring Boot configuration sources: test each deployment path, not a guessed order, Spring Boot property override: fail startup on the effective value.
Checked file and migration continuation
Continue with Spring Boot file versus command line: assert the bound result.
