What is the configuration precedence order in Spring Boot?
Spring Boot loads configuration properties in a specific order where properties defined later override earlier ones. The order is: 1) Command line arguments, 2) Java System properties, 3) OS environment variables, 4) Application properties inside jar (application.properties or application.yml), 5) Default properties.
Click to reveal answer
beginner
Which configuration source has the highest priority in Spring Boot?
Command line arguments have the highest priority and override all other configuration sources.
Click to reveal answer
intermediate
How do environment variables rank in Spring Boot's configuration precedence?
Environment variables have lower priority than command line arguments and Java system properties but higher than application properties inside the jar.
Click to reveal answer
intermediate
What happens if the same property is defined in both application.properties and as a system property?
The system property value overrides the value defined in application.properties because system properties have higher precedence.
Click to reveal answer
beginner
Why is understanding configuration precedence important in Spring Boot?
It helps you know which configuration value will be used when the same property is defined in multiple places, avoiding confusion and bugs.
Click to reveal answer
Which configuration source has the lowest priority in Spring Boot?
AJava System properties
BApplication properties inside jar
CCommand line arguments
DEnvironment variables
✗ Incorrect
Application properties inside the jar have lower priority than command line arguments, system properties, and environment variables.
If a property is set both as an environment variable and a command line argument, which one is used?
ACommand line argument
BDepends on OS
CEnvironment variable
DBoth are merged
✗ Incorrect
Command line arguments have higher precedence than environment variables.
Where does Spring Boot look for application.properties by default?
AOnly in external config folder
BOnly in command line arguments
COnly in environment variables
DInside the jar file
✗ Incorrect
Spring Boot loads application.properties or application.yml inside the jar by default.
Which of these is NOT a valid configuration source in Spring Boot?
ACommand line arguments
BJava System properties
CDatabase configuration table
DEnvironment variables
✗ Incorrect
Spring Boot does not load configuration from a database table by default.
If a property is missing in all sources, what does Spring Boot do?
AUses a default value if defined
BThrows an error
CAsks user input
DSkips the property silently
✗ Incorrect
Spring Boot uses default values if defined, otherwise the property remains unset.
Explain the order in which Spring Boot loads configuration properties and why this order matters.
Think about where you can set properties and which one wins if duplicates exist.
You got /6 concepts.
Describe a scenario where understanding configuration precedence can help avoid bugs in a Spring Boot application.
Consider when a developer sets a property in environment and also in command line.
You got /4 concepts.
Practice
(1/5)
1. In Spring Boot, which configuration source has the highest priority and overrides all others?
easy
A. Environment variables
B. Command line arguments
C. application.properties file
D. Default properties in code
Solution
Step 1: Understand configuration sources in Spring Boot
Spring Boot loads configuration from multiple sources like properties files, environment variables, and command line arguments.
Step 2: Identify the highest priority source
Command line arguments have the highest priority and override all other configuration sources.
Final Answer:
Command line arguments -> Option B
Quick Check:
Highest priority = Command line arguments [OK]
Hint: Remember: command line args always override others [OK]
Common Mistakes:
Thinking application.properties has highest priority
Confusing environment variables with command line args
Assuming default properties override others
2. Which of the following is the correct way to specify a property in Spring Boot's application.properties file?
easy
A. server.port=8080
B. server-port:8080
C. server_port=8080
D. serverPort=8080
Solution
Step 1: Recall Spring Boot property syntax
Spring Boot uses dot notation in application.properties files, like server.port=8080.
Step 2: Check each option's syntax
server.port=8080 uses correct dot notation. Options B, C, and D use invalid separators or camelCase which are not valid in properties files.
Final Answer:
server.port=8080 -> Option A
Quick Check:
Properties use dot notation = server.port=8080 [OK]
Hint: Use dot notation for properties in application.properties [OK]
Common Mistakes:
Using underscores instead of dots
Using colon instead of equals sign
Using camelCase instead of dot notation
3. Given the following Spring Boot configuration sources: application.properties sets server.port=8080 Environment variable SERVER_PORT=9090 Command line argument --server.port=7070 What port will the application run on?
medium
A. 7070
B. 9090
C. Default port 8081
D. 8080
Solution
Step 1: List configuration sources and their values
application.properties: 8080, environment variable: 9090, command line argument: 7070.
Step 2: Apply Spring Boot precedence order
Command line arguments override environment variables and properties files, so port is 7070.
Final Answer:
7070 -> Option A
Quick Check:
Command line args override others = 7070 [OK]
Hint: Command line args always win over env and properties [OK]
Common Mistakes:
Choosing environment variable value instead of command line
4. You set server.port=8080 in application.properties and also pass --server.port=9090 as a command line argument, but the app still runs on port 8080. What is the most likely cause?
medium
A. application.properties has higher priority
B. Spring Boot ignores command line arguments by default
C. Environment variable overrides command line
D. Command line argument syntax is incorrect
Solution
Step 1: Understand expected behavior
Command line arguments should override properties file settings.
Step 2: Identify why command line arg might be ignored
If the syntax is wrong (e.g., missing dashes or wrong format), Spring Boot won't apply it.
Final Answer:
Command line argument syntax is incorrect -> Option D
Quick Check:
Incorrect syntax means command line ignored [OK]
Hint: Check command line argument format carefully [OK]
Common Mistakes:
Assuming properties file always overrides command line
Thinking environment variables override command line
Believing Spring Boot ignores command line args by default
5. You want to set a default property app.mode=dev in your Spring Boot app but allow it to be overridden by environment variables or command line arguments. Which order of precedence correctly applies?
hard
A. Environment variables < Command line arguments < application.properties < Default properties
B. Command line arguments < Environment variables < application.properties < Default properties
C. Default properties < application.properties < Environment variables < Command line arguments
D. application.properties < Default properties < Command line arguments < Environment variables
Solution
Step 1: Understand default property placement
Default properties are lowest priority and can be overridden by all other sources.
Step 2: Recall Spring Boot precedence order
Order is: Default properties < application.properties < Environment variables < Command line arguments.
Final Answer:
Default properties < application.properties < Environment variables < Command line arguments -> Option C
Quick Check:
Lowest to highest priority = Default properties < application.properties < Environment variables < Command line arguments [OK]
Hint: Remember: default < properties < env vars < command line [OK]
Common Mistakes:
Mixing order of environment variables and properties file