Skip to content

Spring Boot Configuration and Effective Values

Interactive Diagram

새 탭 ↗

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.

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.

KindExampleLocation
Common defaultsApplication name, safe log levelConfiguration packaged in the jar
Deployment-specific valuesAPI address, timeoutExternal files or deployment environment variables
SecretsPassword, API tokenRestricted secret storage and injection path

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.

  1. Default configuration packaged in the jar
  2. Profile-specific configuration packaged in the jar
  3. Default configuration outside the jar
  4. Profile-specific configuration outside the jar
  5. Operating system environment variables
  6. JVM system properties
  7. SPRING_APPLICATION_JSON
  8. 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.

src/main/resources/application.properties
spring.application.name=tutorial
spring.profiles.default=local
app.mode=base
src/main/resources/application-prod.properties
app.mode=prod

spring.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.

src/main/java/com/example/tutorial/SettingsController.java
package com.example.tutorial;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public 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.

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.

Terminal window
mvn package
java -jar target/tutorial-1.0.0.jar
Terminal window
java -jar target/tutorial-1.0.0.jar --spring.profiles.active=prod
Terminal window
APP_MODE=environment java -jar target/tutorial-1.0.0.jar --spring.profiles.active=prod
Terminal window
APP_MODE=environment java -Dapp.mode=system -jar target/tutorial-1.0.0.jar \
--spring.profiles.active=prod --app.mode=command
Terminal window
curl http://localhost:8080/settings
RunExpected 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.

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.