Spring Boot Config Priority
Spring Boot loads configuration from multiple sources and applies overrides by priority. Higher-priority sources override lower ones. This article explains common sources, their order, and how to debug "config not taking effect" with a reference table.
Overview
- Sources: Command-line arguments,
SPRING_APPLICATION_JSON, Servlet init params, system properties,application-{profile}.yml/.properties,application.yml/.properties,@PropertySource, and default values. - Priority: Higher to lower. Command line overrides env vars, which override profile-specific files, which override the base
applicationfiles. - Profiles: Activated via
spring.profiles.active(env, system property, or config).application-prod.ymlis loaded when theprodprofile is active and overridesapplication.yml.
Example
Example 1: Common priority (high to low)
| Source | Description |
|---|---|
Command line --key=value | Highest; overrides everything else |
| Environment variables | SPRING_APPLICATION_JSON, SERVER_PORT, etc. |
| System properties | -Dserver.port=9090 |
| application-{profile}.yml | Loaded when profile is active |
| application.yml | Base config, lowest priority |
Example 2: Override example
YAML# application.yml server: port: 8080 # application-prod.yml (when spring.profiles.active=prod) server: port: 9090 # Command line --server.port=9091 overrides both
- Production config often lives in
application-prod.ymlor external config so it is not committed to the repo.
Example 3: Externalized config
config/directory next to the jar (or current working directory)application.ymlnext to the jar--config.location=file:/path/to/config/- Lets you deploy the same jar with different config per environment without rebuilding.
Example 4: Full property source order (condensed)
- Command-line arguments
SPRING_APPLICATION_JSON(env or system property)- ServletConfig init params
- ServletContext init params
- JNDI attributes
- Java system properties (
-D) - OS environment variables
application-{profile}.yml(for active profiles)application.yml(or.properties)@PropertySourceon your@Configurationclasses- Defaults (e.g.
ServerPropertiesdefaults)
Example 5: Relaxed binding
server.port,server_port, andSERVER_PORTall map toserver.port.- Env vars typically use
UPPER_SNAKE_CASE; config files uselowercaseorkebab-case. - Spring normalizes these to the canonical property name.
Example 6: Profile-specific overrides
YAML# application.yml logging: level: root: INFO # application-dev.yml logging: level: root: DEBUG org.springframework: TRACE
- When
spring.profiles.active=dev, the dev overrides apply. Use profiles for dev, test, prod, etc.
Core Mechanism / Behavior
- PropertySources: Spring builds an ordered
PropertySourceslist. The first occurrence of a property wins; higher-priority sources are checked first. - Profile-specific files:
application-{profile}is loaded only when that profile is active. Multiple profiles can be active; later profiles override earlier ones for the same property. - Relaxed binding: Converts between formats so
SERVER_PORTandserver.portrefer to the same property. - Type conversion: Strings are converted to the target type (int, boolean, Duration, etc.) when bound to
@ConfigurationProperties.
Debugging "Config Not Taking Effect"
- Check priority: Command line and env vars override files. If you set
server.portinapplication.ymlbut also haveSERVER_PORTin the env, the env wins. - Check profile: Is the expected profile active?
spring.profiles.activemight be set by the runtime (e.g. cloud, K8s). - Check property name: Typos and wrong nesting (e.g.
server.portvsserverport) cause silent ignores. Usedebug=trueor actuator/actuator/configpropsto inspect. - Check type: Wrong type (e.g. string where number expected) can cause binding to fail or fall back to defaults.
Key Rules
- Sensitive data (passwords, keys): Use env vars or a secrets manager; do not put in
application.ymlcommitted to the repo. - Use profiles for dev/test/prod; production should use profile-specific files or external config.
- Knowing the priority order helps debug "config not taking effect"; command line and env vars override file-based config.
- Prefer
application.ymloverapplication.propertiesfor nested structure; both work.
What's Next
See Config Center for dynamic config and runtime overrides. See Spring Boot Startup for when config is loaded during startup.