Configuration and profiles
Junior Frequently asked 4 min
On this page
The question
The same property is set in several places. Which value does Spring Boot use?
30-second answer
Spring Boot layers its sources, and the highest one that sets a key wins, per key. Command-line arguments beat environment variables, which beat the active profile's file, which beats application.yml. So a container can override a packaged default with an environment variable, and the profile file only needs the keys that differ.
Understand it
Which source sets the value?
Exploreapp.timeout is 30s. The file of the active profile overrides the base file, but only for the keys it sets.
View as text
Four stacked sources of the property app.timeout, highest precedence on top: the command line, the APP_TIMEOUT environment variable, the active profile's file and application.yml. Each shows its value or says it is not set. The winner is highlighted and the others are marked as overridden.
app.timeout is 5s. No profile and no override, so the value from the base file applies. It is the default for every environment.
app.timeout is 30s. The file of the active profile overrides the base file, but only for the keys it sets.
app.timeout is 60s. An environment variable beats every file. This is how a container or a Kubernetes pod overrides the packaged defaults.
app.timeout is 1s. A command-line argument beats everything below it, which makes it handy for a one-off override.
Four stacked sources of the property app.timeout, highest precedence on top: the command line, the APP_TIMEOUT environment variable, the active profile's file and application.yml. Each shows its value or says it is not set. The winner is highlighted and the others are marked as overridden.
app.timeout is 5s. No profile and no override, so the value from the base file applies. It is the default for every environment.
app.timeout is 30s. The file of the active profile overrides the base file, but only for the keys it sets.
app.timeout is 60s. An environment variable beats every file. This is how a container or a Kubernetes pod overrides the packaged defaults.
app.timeout is 1s. A command-line argument beats everything below it, which makes it handy for a one-off override.
Takeaway: Spring Boot layers its sources: command line, then environment, then the profile file, then the base file. The highest source that sets a key wins, per key.
In code
@ConfigurationProperties("app.payment")
@Validated
public record PaymentProperties(
@NotBlank String url,
Duration timeout,
@Min(1) int maxAttempts) {
}
@Configuration
@EnableConfigurationProperties(PaymentProperties.class)
class PaymentConfig {
}
// One value, with a default, and no type-safe grouping
@Component
class Banner {
Banner(@Value("${app.banner:Welcome}") String text) {
System.out.println(text);
}
}payment:
timeout: 5s
---
spring:
config:
activate:
on-profile: prod
payment:
timeout: 2sPAYMENT_TIMEOUT=10s java -jar shop.jar # binds to payment.timeout
java -jar shop.jar --payment.timeout=15s # a command-line argument wins over bothSolve it
Production uses a 30-second timeout although application-prod.yml says 2 seconds
Payments hang for 30 seconds in production. application-prod.yml sets payment.timeout: 2s, the prod profile is
active, and nobody remembers setting 30 seconds. Find where the value comes from.
- Recall the order. Command-line arguments beat environment variables, which beat profile files, which beat
application.yml. Any higher source silently wins. - Ask the running app.
/actuator/env/payment.timeoutnames the property source of the value. Values are masked unlessmanagement.endpoint.env.show-valuesallows them, but the source is shown. - Read the answer. The source is
systemEnvironment: the Kubernetes deployment setsPAYMENT_TIMEOUT=30s, left from an old incident. - Fix the ownership. Remove the variable, or make the deployment the only place that sets it. One value should have one owner.
- Check. After the rollout, the endpoint shows
application-prod.ymlas the source.
curl -s localhost:8080/actuator/env/payment.timeout
# {"property": {"source": "systemEnvironment", "value": "******"}, ...}env:
- name: PAYMENT_TIMEOUT
value: "30s" # beats application-prod.yml; delete it or make it the only sourceHow to answer
- Sources merge per key, so a profile file does not replace the whole base file.
- Test properties such as
@TestPropertySourcerank above all of these. - With both in one location,
application.propertiesbeatsapplication.yml. /actuator/envshows which source won, with values masked.
Common mistakes
- Thinking a profile file replaces
application.ymlentirely. - Saying a file always beats an environment variable.
Follow-up questions
What is application.yml for, and where does Spring Boot look for it?
It holds externalized configuration: settings kept out of the code, so one build runs everywhere. Spring Boot loads it from the classpath root and /config, and from the current directory and its config folder, with outside files overriding packaged ones. YAML and properties are equivalent. Secrets belong in the environment or a vault, not in the file.
- One artifact, many environments, is a Twelve-Factor practice.
spring.config.importpulls in more files or a config server.- A
---line splits one YAML file into several documents.
What are Spring profiles, and what can they change?
A profile is a named set of configuration you can switch on. Activate it with spring.profiles.active, usually from the environment. It loads application-{profile}.yml, and it turns on beans marked @Profile("dev"). It suits differences between environments, such as a fake mail sender in development. Do not branch business logic on a profile.
spring.profiles.activeis invalid inside a profile-specific file, and startup fails.spring.profiles.groupactivates several profiles under one name.- Too many profiles make it hard to know what production runs.
@Bean
@Profile("dev")
MailSender fakeMail() {
return new LoggingMailSender();
} What is the difference between @Value and @ConfigurationProperties?
@Value injects one property, with an optional default and SpEL. @ConfigurationProperties binds a whole prefix into a typed class or record, with relaxed binding, nesting and validation through @Validated. Use it for any group of settings, so they live in one place and fail at startup when wrong. Keep @Value for a single, simple value.
- A missing
@Valuewith no default fails the startup with a placeholder error. @Value("${app.retry.maxAttempts}")fails if the file saysmax-attempts: use the kebab-case form.- SpEL works in
@Valueonly.
@ConfigurationProperties("app.retry")
@Validated
record RetryProperties(@Min(1) int maxAttempts, Duration delay) {} With spring.profiles.active=dev,prod, which one wins for a key set in both?
The last one listed, so prod here. Reversing the list reverses the result. Both are active, and both files apply, so keys set in only one of them still take effect.
How does an environment variable map to a property name?
Through relaxed binding: uppercase, with dots replaced by underscores and dashes removed. app.retry.max-attempts is read from APP_RETRY_MAXATTEMPTS. It works for @ConfigurationProperties, and for @Value when the placeholder uses the canonical kebab-case form.
Check yourself
Show answers
- 60s: Correct. An environment variable ranks above every configuration file, including the active profile's.
- 2s: Incorrect. The profile file beats application.yml, but not the environment.
- Startup fails with a conflict.: Incorrect. Several sources setting one key is normal. The precedence order decides.
Show answers
- The value from application.yml: Correct. Sources merge per key, so the profile file only overrides what it sets.
- Nothing, because the profile replaced the file: Incorrect. A profile file adds to the base file. It does not replace it.
- Startup fails, because dev does not set a.: Incorrect. A profile file does not have to repeat any key.