Quick revision
The short answer
The command line argument wins, so the server starts on port 8083. Spring Boot reads properties from many sources and gives each a fixed rank; from high to low the main ones are command line arguments, SPRING_APPLICATION_JSON, Java system properties, operating system environment variables, then the config data files such as application.yml. Test sources such as @TestPropertySource rank above the command line, and @PropertySource and default properties rank at the very bottom.
What to remember
In this answer, we saw that Spring Boot keeps an ordered list of property sources and returns the first value it finds for a key, so in the example the command line argument wins and the port is 8083. The main order from high to low is test sources, command line arguments, SPRING_APPLICATION_JSON, Java system properties, environment variables and config files, with @PropertySource and default properties at the bottom. Within config files, files outside the jar beat packaged ones and profile-specific files beat plain ones.
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 command line argument wins, so the server starts on port 8083. Spring Boot reads properties from many sources and gives each a fixed rank; from high to low the main ones are command line arguments, SPRING_APPLICATION_JSON, Java system properties, operating system environment variables, then the config data files such as application.yml. Test sources such as @TestPropertySource rank above the command line, and @PropertySource and default properties rank at the very bottom.
Why this question is asked
Almost every Spring Boot service gets its configuration from more than one place: a file in the jar, environment variables set by the deployment, and sometimes a flag added by an operator in a hurry. When the same property appears in several of them, we need to know which one Spring uses, or we spend hours wondering why a change has no effect. Interviewers ask this because it shows whether we understand Spring Boot's Environment and can reason about production behaviour. In this answer, we will recap what a property source is, work through the example, look at the full precedence order, see how config files rank among themselves, and finish with where tests fit in.
1A Quick Recap of Property Sources
Spring keeps all configuration in an object called the Environment. Inside it is an ordered list of property sources. A property source is simply something that can answer the question what is the value of this key: the command line arguments, the environment variables of the process, the Java system properties, an application.yml file, and so on.
When code asks for server.port, Spring walks this list from the top and returns the first value it finds. Sources lower in the list are not merged in for that key; they are simply never reached. So precedence is just the order of the list, and Spring Boot fixes that order so that more specific, more deliberate settings sit higher than general defaults.
2Working Through the Example
We have the same key in three places. The jar contains application.yml with server.port: 8081. The deployment sets the environment variable SERVER_PORT=8082, which Spring Boot maps to server.port through relaxed binding (upper case, underscores instead of dots). And the process is started with a command line flag.
Spring looks for server.port from the top of the list. Command line arguments are near the top, so it finds 8083 there and stops. The environment variable and the file are never consulted for this key. If we removed the --server.port flag, the search would continue down to the environment variables and find 8082; if we also removed SERVER_PORT, it would reach application.yml and use 8081.
The reason for this order is intent. A file in the jar is a default chosen when the code was written. An environment variable is chosen by whoever deployed the service. A command line flag is the most specific and deliberate choice, made for this one process, so it should win.
3The General Precedence Order
The full list in the Spring Boot documentation has more entries, but the ones we meet in practice are shown below, from highest to lowest. JNDI and servlet init parameters sit between system properties and SPRING_APPLICATION_JSON but rarely matter in a jar-based service. SPRING_APPLICATION_JSON is an environment variable or system property that holds a whole block of JSON configuration, handy when a platform wants to inject many values in one go.
| Rank | Source | Example |
|---|---|---|
| 1 | Devtools global settings (dev only) | ~/.config/spring-boot/... |
| 2 | @TestPropertySource | @TestPropertySource(properties = "server.port=0") |
| 3 | @DynamicPropertySource in tests | registry.add("db.url", container::getJdbcUrl) |
| 4 | properties attribute of test annotations | @SpringBootTest(properties = "x=1") |
| 5 | Command line arguments | --server.port=8083 |
| 6 | SPRING_APPLICATION_JSON | SPRING_APPLICATION_JSON='{"server":{"port":8084}}' |
| 7 | Java system properties | -Dserver.port=8085 |
| 8 | OS environment variables | SERVER_PORT=8082 |
| 9 | random.* values | ${random.int} |
| 10 | Config data files | application.yml, application-prod.yml |
| 11 | @PropertySource on @Configuration classes | @PropertySource("classpath:extra.properties") |
| 12 | Default properties | app.setDefaultProperties(...) |
Notice two entries that surprise people. Java system properties rank above environment variables, so a -D flag hidden in JAVA_TOOL_OPTIONS beats the SERVER_PORT variable the team thinks is in charge. And @PropertySource ranks below application.yml, because Spring Boot only adds those sources after the context starts processing configuration classes, which is too late to affect some early properties such as logging settings.
4How Config Files Rank Among Themselves
Config data files are a whole group, and there is an order inside that group too. Files outside the jar override files packaged inside it, so an operator can drop an application.yml next to the jar, or in a ./config folder, to override the packaged defaults without rebuilding. And profile-specific files, such as application-prod.yml, override the plain application.yml at the same level.
If the same location holds both application.properties and application.yml, the .properties file wins; it is simpler to keep one format per project so that nobody has to remember that rule. Whatever the files say, all of them still rank below environment variables and command line arguments.
5Where Tests Fit In
Tests often need to override values that production sets in the environment, for example to use a random port or a test database. That is why the test sources sit at the very top, above even command line arguments. A value from @TestPropertySource, from the properties attribute of @SpringBootTest, or from a @DynamicPropertySource method, such as the JDBC URL of a Testcontainers database, therefore always reaches the test, even on a CI machine that happens to have SERVER_PORT set.
6Follow-up questions
How can I see which source actually supplied a value?
Use the Actuator env endpoint: GET /actuator/env/server.port lists every source that contains the key, in precedence order, and shows which one won. In Spring Boot 3 the values are masked by default, but the source names are still visible, which is usually enough to answer the question.
Can a command line argument be disabled?
Yes. SpringApplication.setAddCommandLineProperties(false) stops Spring Boot from turning --key=value arguments into a property source. Some platforms do this so that only the deployment configuration controls the service.
Why does @PropertySource rank so low?
Because Spring Boot reads its config data before the application context exists, while @PropertySource is only processed when configuration classes are parsed, later in startup. By then the main sources are already in place, so the annotation's file is added at the end of the list and cannot override them.
7Conclusion
In this answer, we saw that Spring Boot keeps an ordered list of property sources and returns the first value it finds for a key, so in the example the command line argument wins and the port is 8083. The main order from high to low is test sources, command line arguments, SPRING_APPLICATION_JSON, Java system properties, environment variables and config files, with @PropertySource and default properties at the bottom. Within config files, files outside the jar beat packaged ones and profile-specific files beat plain ones.
Open with
The port is 8083, because the command line argument has the highest precedence of the three. Spring Boot's Environment holds an ordered list of property sources and returns the first value it finds for a key, so once it finds server.port in the command line arguments it never looks at the environment variable or the file.
Then, if they want more
From high to low, the common order is this. Devtools global settings and the test sources come first: @TestPropertySource, @DynamicPropertySource and the properties attribute of @SpringBootTest. Then command line arguments, then SPRING_APPLICATION_JSON, then Java system properties, then operating system environment variables like SERVER_PORT, and then the config data files.
At the bottom are @PropertySource on configuration classes and the default properties set on SpringApplication. Inside the config files there is a second ordering. Files outside the jar override the ones packaged inside it, and profile-specific files like application-prod.yml override the plain application.yml at the same level.
The reasoning is that the more deliberate and more specific a setting is, the higher it ranks. A packaged file is a default, an environment variable is the deployment's choice, and a command line flag is a choice for this one process.
If I needed to confirm where a value came from in a running service, I would use the Actuator env endpoint, which lists the sources in order.
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 Boot reference
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 "server.port is set to 8081 in application.yml in the jar, to 8082 by the SERVER_PORT...": https://scrivio-interview-library.netlify.app/spring-boot/externalized-configuration/4bb96d522b/