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?

Explore

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

Sources from highest to lowest precedenceCommand-line arguments beat environment variables, which beat the active profile's file, which beats the base application file. The highest source that sets a key wins, and each key is resolved separately.Command-lineargumentsEnvironmentvariablesapplication-prod.ymlapplication.yml
Sources from highest to lowest precedenceCommand-line arguments beat environment variables, which beat the active profile's file, which beats the base application file. The highest source that sets a key wins, and each key is resolved separately.Command-lineargumentsEnvironmentvariablesapplication-prod.ymlapplication.yml
Sources from highest to lowest precedence

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);
    }
}
Typed settings, checked at startup
Where the payment settings come fromThe url comes from application.yml and is overridden in production by the APP_PAYMENT_URL environment variable. All of it binds into one PaymentProperties record, and a validation failure stops the startup.application.yml:app.payment.urlPaymentPropertiesAPP_PAYMENT_URLinproductionValidated at startup
Where the payment settings come fromThe url comes from application.yml and is overridden in production by the APP_PAYMENT_URL environment variable. All of it binds into one PaymentProperties record, and a validation failure stops the startup.application.yml:app.payment.urlPaymentPropertiesAPP_PAYMENT_URLinproductionValidated at startup
Where the payment settings come from
payment:
  timeout: 5s
---
spring:
  config:
    activate:
      on-profile: prod
payment:
  timeout: 2s
One application.yml, with a section that only the prod profile activates
PAYMENT_TIMEOUT=10s java -jar shop.jar        # binds to payment.timeout
java -jar shop.jar --payment.timeout=15s      # a command-line argument wins over both
Relaxed binding: an environment variable or an argument overrides the file

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

  1. Recall the order. Command-line arguments beat environment variables, which beat profile files, which beat application.yml. Any higher source silently wins.
  2. Ask the running app. /actuator/env/payment.timeout names the property source of the value. Values are masked unless management.endpoint.env.show-values allows them, but the source is shown.
  3. Read the answer. The source is systemEnvironment: the Kubernetes deployment sets PAYMENT_TIMEOUT=30s, left from an old incident.
  4. Fix the ownership. Remove the variable, or make the deployment the only place that sets it. One value should have one owner.
  5. Check. After the rollout, the endpoint shows application-prod.yml as the source.
curl -s localhost:8080/actuator/env/payment.timeout
# {"property": {"source": "systemEnvironment", "value": "******"}, ...}
Ask which source supplied the value
env:
  - name: PAYMENT_TIMEOUT
    value: "30s"        # beats application-prod.yml; delete it or make it the only source
The Kubernetes deployment entry that overrode the file

How to answer

  • Sources merge per key, so a profile file does not replace the whole base file.
  • Test properties such as @TestPropertySource rank above all of these.
  • With both in one location, application.properties beats application.yml.
  • /actuator/env shows which source won, with values masked.

Common mistakes

  • Thinking a profile file replaces application.yml entirely.
  • 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.import pulls in more files or a config server.
  • A --- line splits one YAML file into several documents.

See it in the infographic: Which source sets the value?

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.active is invalid inside a profile-specific file, and startup fails.
  • spring.profiles.group activates several profiles under one name.
  • Too many profiles make it hard to know what production runs.
@Bean
@Profile("dev")
MailSender fakeMail() {
    return new LoggingMailSender();
}

See it in the infographic: Which source sets the value?

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 @Value with no default fails the startup with a placeholder error.
  • @Value("${app.retry.maxAttempts}") fails if the file says max-attempts: use the kebab-case form.
  • SpEL works in @Value only.
@ConfigurationProperties("app.retry")
@Validated
record RetryProperties(@Min(1) int maxAttempts, Duration delay) {}

See it in the infographic: Which source sets the value?

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

1. application-prod.yml sets app.timeout to 2s, and the prod profile is active. The environment variable APP_TIMEOUT is 60s. Which value is used?
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.
2. application.yml sets a and b. application-dev.yml sets only b. The dev profile is active. What does a resolve to?
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.