Quick revision
The short answer
The Environment is Spring's view of configuration: it knows the active profiles and holds an ordered list of PropertySources, and when we ask for a key, the first source that contains it wins. In Spring Boot, command-line arguments have higher priority than Java system properties, which beat OS environment variables, which beat the application files. Among the files, profile-specific ones override plain ones and files outside the jar override those inside it, while @PropertySource and default properties have low priority.
What to remember
In this answer, we saw that the Spring Environment holds the active profiles and an ordered list of PropertySources, and that a lookup returns the value from the first source containing the key. In Spring Boot, command-line arguments beat Java system properties, which beat environment variables, which beat the application files; among the files, profile-specific beats plain and outside the jar beats inside it, while @PropertySource and default properties come last. The actuator env endpoint shows which source supplied each value.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The Environment is Spring's view of configuration: it knows the active profiles and holds an ordered list of PropertySources, and when we ask for a key, the first source that contains it wins. In Spring Boot, command-line arguments have higher priority than Java system properties, which beat OS environment variables, which beat the application files. Among the files, profile-specific ones override plain ones and files outside the jar override those inside it, while @PropertySource and default properties have low priority.
Why this question is asked
Almost every configuration surprise in a Spring Boot application, a value that will not change, a test that picks up production settings, a variable that seems ignored, comes down to which property source won. Interviewers ask this to see whether we can reason about that order instead of guessing, because the order is also what lets one artifact run in every environment. In this answer, we will recap what the Environment and a PropertySource are, see how a lookup walks the list, go through Spring Boot's order from highest to lowest, look closely at the application files, and see how to check which source supplied a value.
1A Quick Recap of the Environment
The Environment is an object every Spring application context has. It answers two questions: which profiles are active, and what is the value of a given property. For the second, it keeps a list of PropertySources. A PropertySource is simply a named collection of key-value pairs: one holds the command-line arguments, one the operating system's environment variables, one the contents of application.yml, and so on.
Spring Boot builds this list when the application starts, before any bean is created, so that configuration can influence which beans exist. We usually read from it indirectly through @Value or @ConfigurationProperties, but we can also inject Environment and call getProperty.
2How a Lookup Walks the List
When we ask for app.timeout, the Environment checks each PropertySource in order and returns the value from the first one that contains the key. It does not merge or compare values; the first match simply wins and the rest are never consulted for that key. So overriding a value means putting it in a source that sits earlier in the list.
Notice that APP_TIMEOUT is found for the key app.timeout. That is Spring Boot's relaxed binding at work: it maps the canonical property name to the upper-case, underscore form that environment variables use.
3Spring Boot's Order From Highest to Lowest
The Spring Boot reference documentation lists the order in full. The part we meet in everyday work is shown below. Test-only sources, such as @TestPropertySource and @DynamicPropertySource, sit at the very top, above command-line arguments, which is why tests can override anything.
| Priority | Property source |
|---|---|
| Highest | Test sources: @DynamicPropertySource, @TestPropertySource |
| Command-line arguments: --app.timeout=20s | |
SPRING_APPLICATION_JSON | |
| Java system properties: -Dapp.timeout=10s | |
OS environment variables: APP_TIMEOUT=8s | |
| Config data: application and application-{profile} files | |
@PropertySource on @Configuration classes | |
| Lowest | Default properties: SpringApplication.setDefaultProperties |
The reasoning behind this order is that the closer a setting is to the moment of launch, the more specific it is. A default in code is the most general; the file in the jar is written by developers for every environment; an environment variable is set by the platform for one deployment; and a command-line argument is typed for one run. More specific settings win.
4How the Application Files Are Ordered
The config data entry in the table is itself a small ordered group, because Spring Boot reads application files from several places. Profile-specific files, such as application-prod.yml, override the plain application.yml. Files outside the jar, in the current directory or a config subdirectory, override files packaged inside the jar. Combining both rules gives four levels.
| File | Where |
|---|---|
application-prod.yml | Outside the jar (./ or ./config/) |
application.yml | Outside the jar |
application-prod.yml | Inside the jar (classpath) |
application.yml | Inside the jar |
This lets the jar carry sensible defaults and profile-specific defaults, while an operator can still drop a file next to the jar to change a value for one machine. Notice that @PropertySource sits below all of these. A file loaded with @PropertySource is added late, after the context starts processing configuration classes, so it cannot override application.yml, and it is too late for properties that Spring Boot reads early, such as logging settings.
5Checking Which Source Won
When a value is not what we expect, guessing the order is slower than asking the application. The actuator env endpoint, at /actuator/env/app.timeout, lists every property source that contains the key, in order, and shows which one supplied the effective value. Since Spring Boot 3 values are masked by default, so we set management.endpoint.env.show-values to when-authorized and protect the endpoint. In a test or debugger, iterating over environment.getPropertySources() and printing each source's name and whether it contains the key gives the same answer.
6Follow-up questions
Why does my value from @PropertySource not override application.yml?
Because @PropertySource files have lower priority than all config data files. They are meant for adding properties, not overriding Boot's files. To override, put the value in a profile-specific file, an external file, an environment variable or a command-line argument, or import the file with spring.config.import, which gives it config data priority.
What is the default profile?
When no profile is active, Spring treats a profile named default as active, so application-default.yml and @Profile("default") beans apply. As soon as any profile is activated, default is no longer active. The name can be changed with spring.profiles.default.
Can I see the list of property sources at startup?
Not by default, but it is easy to log: an ApplicationListener for ApplicationEnvironmentPreparedEvent, or a simple bean that injects ConfigurableEnvironment, can print the names of its property sources in order. The actuator env endpoint shows the same list on a running application.
7Conclusion
In this answer, we saw that the Spring Environment holds the active profiles and an ordered list of PropertySources, and that a lookup returns the value from the first source containing the key. In Spring Boot, command-line arguments beat Java system properties, which beat environment variables, which beat the application files; among the files, profile-specific beats plain and outside the jar beats inside it, while @PropertySource and default properties come last. The actuator env endpoint shows which source supplied each value.
Open with
The Environment is the object in every Spring context that knows the active profiles and holds an ordered list of property sources, each a named set of key-value pairs: command-line arguments, system properties, environment variables, the application files and so on.
Then, if they want more
When I ask for a key, it walks the list and the first source containing the key wins; nothing is merged.
In Spring Boot the everyday order, from highest to lowest, is test sources like TestPropertySource, then command-line arguments, then SPRING_APPLICATION_JSON, then Java system properties, then OS environment variables, then the application files, then PropertySource annotations, and finally default properties set on SpringApplication.
Within the application files, a profile-specific file such as application-prod.yml overrides application.yml, and files outside the jar override those packaged inside it, so there are four levels. The idea is that the setting closest to the launch is the most specific and wins: the jar carries defaults, the platform sets environment variables, and a command-line argument is for one run.
Environment variables use relaxed names, so APP_TIMEOUT supplies app.timeout. When a value surprises me, I do not guess; I look at the actuator env endpoint for that key, which lists every source containing it and which one won, with values shown only to authorised users.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- 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.
- Official documentation
- Spring Framework reference, Core technologies
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 "What is the Spring Environment, and in what order does Spring Boot resolve a property...": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/63ad396ea0/