Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How does Spring Boot resolve configuration from application.yml, profiles, environment variables and command-line arguments, and which wins?

Spring Boot collects configuration into an ordered list of property sources and, for any key, the source with the highest precedence wins. Command-line arguments beat Java system properties, which beat environment variables, which beat values from application.yml and application.properties files. Among the files, a profile-specific file such as application-prod.yml overrides the default file, and a file outside the jar overrides one packaged inside it. Relaxed binding lets the environment variable SPRING_DATASOURCE_URL set spring.datasource.url, and @ConfigurationProperties with validation gives us type-safe, checked configuration.

Why this question is asked

Almost every production incident involving configuration starts with someone asking why the service uses that value. The answer is always in the precedence order: a forgotten environment variable, a profile that was not active, or a command-line flag in a deployment manifest. Interviewers ask this to see whether we know the order well enough to predict which value wins. In this answer, we will recap property sources, walk through the order with a worked example, look at profile-specific files and relaxed binding, and see how to bind configuration safely with @ConfigurationProperties.

1A Quick Recap of Property Sources

Spring's Environment holds a list of property sources. A property source is any place that maps keys to values: a YAML file, the operating system's environment variables, the JVM's system properties, the command-line arguments. When code asks for spring.datasource.url, Spring walks the list in order of precedence and returns the first value it finds. So a value is never merged from several places; one source wins, and the others are ignored for that key.

This design lets us package sensible defaults in the jar and override only what differs per environment, without editing the jar.

2The Order, From Highest to Lowest

The full list in the Spring Boot documentation has more than a dozen entries, including test-only ones, but the ones we meet in production are few. From highest precedence to lowest: command-line arguments, then the JSON in SPRING_APPLICATION_JSON, then Java system properties (-D flags), then operating system environment variables, then config data files (application.properties and application.yml, with their own internal order), and finally @PropertySource annotations and default properties.

Step throughWho wins for the same key (top wins)
  1. Command-line arguments--server.port=9000
  2. SPRING_APPLICATION_JSONInline JSON in an env var or property
  3. Java system properties-Dserver.port=8500
  4. OS environment variablesSERVER_PORT=8200
  5. Profile-specific filesapplication-prod.yml, outside jar then inside
  6. Default config filesapplication.yml, outside jar then inside
  7. @PropertySource and defaultsLowest precedence

7 layers, from the top down. Step through them one at a time.

Simplified from the documented list; check the reference for the full order.

3A Worked Example

Suppose the same key is set in four places. We can predict the result by reading the stack from the top.

Java
# inside the jar: application.yml
server.port: 8080
# inside the jar: application-prod.yml
server.port: 8100
# started like this:
export SPRING_PROFILES_ACTIVE=prod
export SERVER_PORT=8200
java -jar orders.jar --server.port=9000
# Tomcat started on port 9000 (command line wins)
java -jar orders.jar
# Tomcat started on port 8200 (environment beats both files)
unset SERVER_PORT; java -jar orders.jar
# Tomcat started on port 8100 (prod profile file beats application.yml)

Notice that the profile file only wins over the default file; it still loses to environment variables and command-line arguments. This is why a value set in a Kubernetes manifest as an environment variable silently overrides whatever the team changes in application-prod.yml, a very common source of confusion.

4Profile-Specific Files and External Files

A profile is a named set of configuration, such as dev or prod, activated with spring.profiles.active. For each active profile, Spring Boot also loads application-{profile}.yml, and its values override the default file. If several profiles are active, the one listed last wins when they disagree.

Files can also live outside the jar, for example in a config/ directory next to it or in a location given by spring.config.location or spring.config.additional-location. External files override packaged ones, and profile-specific files override non-profile files, so the effective order of files is: packaged default, packaged profile-specific, external default, external profile-specific, with the last one winning. One more detail: if both application.properties and application.yml exist in the same place, the .properties file takes precedence, so it is best to use one format per project.

5Relaxed Binding for Environment Variables

Environment variable names cannot contain dots or dashes on most systems, so Spring Boot maps them by convention. To turn a property name into an environment variable, we replace dots with underscores, remove dashes and convert to upper case. Indexes in lists become numbers between underscores.

Java
spring.datasource.url -> SPRING_DATASOURCE_URL
spring.datasource.hikari.maximum-pool-size -> SPRING_DATASOURCE_HIKARI_MAXIMUMPOOLSIZE
my.servers[0].host -> MY_SERVERS_0_HOST
export SPRING_DATASOURCE_URL=jdbc:postgresql://db.prod:5432/orders
# environment.getProperty("spring.datasource.url")
# -> jdbc:postgresql://db.prod:5432/orders

Relaxed binding works in the other direction too: in files, maximum-pool-size, maximumPoolSize and maximum_pool_size all bind to the same field. The kebab-case form is the recommended one for files.

6Type-Safe Configuration With @ConfigurationProperties

Reading values one by one with @Value works, but it scatters string keys through the code and only fails when a value is missing, not when it is wrong. @ConfigurationProperties binds a whole group of keys to a typed object, and with @Validated, Bean Validation checks it at startup.

Java
@Validated
@ConfigurationProperties(prefix = "payments")
public record PaymentsProperties(
@NotNull URI baseUrl,
@NotNull @DurationMin(millis = 100) Duration timeout,
@Min(1) @Max(50) int maxRetries) {}
@SpringBootApplication
@ConfigurationPropertiesScan
class OrdersApplication { }
# application.yml
payments:
base-url: https://payments.internal
timeout: 2s # converted to Duration.ofSeconds(2)
max-retries: 0
# startup fails: Binding to target ... failed: payments.maxRetries must be greater than or equal to 1

The reason this matters is that a bad value is caught when the application starts, during deployment, instead of in the middle of the night when the code path finally runs. Spring Boot's configuration metadata also lets IDEs auto-complete these keys. @DurationMin comes from Hibernate Validator; plain Jakarta constraints such as @NotNull and @Min work with any provider.

7Follow-up questions

How can I see which property source a value came from?

The Actuator env endpoint lists every property source in order with its values, and the configprops endpoint shows the bound @ConfigurationProperties beans. Since Spring Boot 3.0, values are masked by default, and management.endpoint.env.show-values controls when they are shown, so the endpoints should be protected and values revealed only to authorised users.

Where does spring.config.import fit in?

spring.config.import lets a config file pull in additional sources, such as another file, a directory tree of mounted files with configtree:, or an external config server. The imported values are treated as config data and take precedence over the file that imported them, but they still rank below environment variables and command-line arguments.

Do environment variables override values in a Kubernetes ConfigMap?

It depends on how the ConfigMap is used. If its entries become environment variables, they are environment variables. If it is mounted as an application.yml file or imported as a config tree, it is config data and ranks below real environment variables, which is a frequent cause of surprise.

8Conclusion

In this answer, we saw that Spring Boot resolves each key from an ordered list of property sources, with command-line arguments above system properties, above environment variables, above the application files. Profile-specific files override the default file, external files override packaged ones, relaxed binding maps names like SPRING_DATASOURCE_URL to spring.datasource.url, and @ConfigurationProperties with @Validated binds configuration into typed objects that fail fast on bad values.

What to say in the interviewAbout 80 seconds aloud

Open with

Spring Boot builds an ordered list of property sources in the Environment, and for each key the first source in precedence order wins; values are not merged. The ones that matter in practice, from highest to lowest, are command-line arguments like --server.port, then SPRING_APPLICATION_JSON, then Java system properties, then operating system environment variables, and then the config files.

Then, if they want more

Among the files, a profile-specific file like application-prod.yml overrides application.yml, and files outside the jar override the ones packaged inside. So if application.yml says port 8080, application-prod.yml says 8100, the environment has SERVER_PORT=8200, and I pass --server.port=9000, the app starts on 9000; without the argument it is 8200.

That is the classic surprise when a Kubernetes manifest sets an environment variable and someone edits the prod YAML and nothing changes. Environment variables map to properties through relaxed binding: dots become underscores, dashes are removed and everything is upper case, so SPRING_DATASOURCE_URL sets spring.datasource.url.

For our own settings I prefer @ConfigurationProperties records with @Validated, so values are typed, grouped and checked at startup, and a bad value fails the deployment instead of failing at runtime. To debug where a value came from, I use the Actuator env endpoint, which is masked by default and should be protected.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.0
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Spring Boot resolve configuration from application.yml, profiles, environment...": https://scrivio-interview-library.netlify.app/spring-boot/production-operations/4355122b53/