Spring Boot Configuration and Effective Values
When a running value differs from a configuration file, first identify the property source that won. Keep the same jar and change its defaults, production profile, environment variables, and command-line arguments to see the resolution order.
Use the Java 17 source level and Spring Boot 3.5.16 from lab setup. Work in a separate copy of the base project for this chapter.
Separate code from configuration
Section titled “Separate code from configuration”Addresses, timeouts, and feature choices that vary by deployment can be external configuration. Business rules shared by all environments belong in code and tests. Manage passwords and tokens separately from ordinary configuration, including their storage, permissions, and rotation.
| Kind | Example | Location |
|---|---|---|
| Common defaults | Application name, safe log level | Configuration packaged in the jar |
| Deployment-specific values | API address, timeout | External files or deployment environment variables |
| Secrets | Password, API token | Restricted secret storage and injection path |
Priority of common property sources
Section titled “Priority of common property sources”The following subset is listed from lower to higher priority. Later sources override the same key from earlier sources. See the official documentation for the complete list, including servlet initialization and test-only sources.
- Default configuration packaged in the jar
- Profile-specific configuration packaged in the jar
- Default configuration outside the jar
- Profile-specific configuration outside the jar
- Operating system environment variables
- JVM system properties
SPRING_APPLICATION_JSON- Command-line arguments
When application.properties and YAML coexist in one location, properties takes precedence. Using one format reduces the number of sources to investigate. Put JVM -D options before -jar and application arguments such as --app.mode after the jar name.
Prepare defaults and a profile
Section titled “Prepare defaults and a profile”spring.application.name=tutorialspring.profiles.default=localapp.mode=baseapp.mode=prodspring.profiles.default=local selects the default when no active profile is specified. An active prod profile replaces that default and loads application-prod.properties. A profile is a configuration group; its name alone does not establish a security boundary.
Do not set spring.profiles.active again inside a profile-specific file. If multiple active profiles define the same key, the profile listed last takes precedence.
Return the effective value
Section titled “Return the effective value”package com.example.tutorial;
import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class SettingsController { private final Environment environment;
public SettingsController(Environment environment) { this.environment = environment; }
public record Settings(String mode) {}
@GetMapping("/settings") public Settings settings() { return new Settings(environment.getRequiredProperty("app.mode")); }}app.mode is a non-sensitive exercise value. getRequiredProperty throws when a required value is absent. If a real service exposes configuration values through an API, define access controls and an explicit disclosure boundary; never return secrets.
Check priority with the same jar
Section titled “Check priority with the same jar”Each launch command below is a separate run. Stop the previous process before starting the next one. Use the final curl command while the server is running. The environment-variable prefix syntax assumes a POSIX shell; use the corresponding syntax in other shells.
mvn packagejava -jar target/tutorial-1.0.0.jarjava -jar target/tutorial-1.0.0.jar --spring.profiles.active=prodAPP_MODE=environment java -jar target/tutorial-1.0.0.jar --spring.profiles.active=prodAPP_MODE=environment java -Dapp.mode=system -jar target/tutorial-1.0.0.jar \ --spring.profiles.active=prod --app.mode=commandcurl http://localhost:8080/settings| Run | Expected response from /settings |
|---|---|
| Default launch | {"mode":"base"} |
prod profile | {"mode":"prod"} |
Profile and APP_MODE | {"mode":"environment"} |
| Environment variable, JVM property, and command-line argument | {"mode":"command"} |
Remove --app.mode=command from the final launch to isolate the JVM property’s priority. The expected value is then system. Without that command-line argument, also injecting SPRING_APPLICATION_JSON='{"app":{"mode":"json"}}' returns json because it takes precedence over the JVM property.
Secret injection and deployment checks
Section titled “Secret injection and deployment checks”A configuration file can refer to an injected key instead of containing the secret itself.
partner.token=${PARTNER_API_TOKEN}The placeholder is not a secret store. Manage the permissions and rotation of the actual provider, whether environment variables, mounted files, or a secret store. Keep secrets out of Kubernetes ConfigMaps; base64 encoding in a Secret is not encryption by itself.
For deployment, inspect the active profiles, external files, container environment variables, and launch arguments together. Trace the winning source instead of hiding unexpected values behind another default. To validate required configuration at startup, configure @ConfigurationProperties with Bean Validation.