Configuration precedence order tells you which settings take priority when multiple sources provide values. It helps your app use the right settings without confusion.
Configuration precedence order in Spring Boot
Spring Boot loads configuration properties from multiple sources in a specific order. The order from highest to lowest priority is: 1. Command line arguments 2. Java System properties (System.getProperties()) 3. OS environment variables 4. Application properties inside your packaged jar (application.properties or application.yml) 5. Default properties (specified programmatically) You can also use profiles to load profile-specific properties like application-dev.properties.
The first source that provides a value for a property wins.
Command line arguments override everything else.
server.port to 8081, overriding any other source.# Run app with command line argument overriding property java -jar app.jar --server.port=8081
SERVER_PORT sets the port to 8082 unless command line overrides it.export SERVER_PORT=8082
java -jar app.jarapplication.properties:
server.port=8080This Spring Boot app reads the server.port property and shows it via a web endpoint. You can test how different configuration sources affect the port value.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController class PortController { @Value("${server.port:default}") private String port; @GetMapping("/port") public String getPort() { return "Server is running on port: " + port; } }
Use --spring.config.location to specify custom config file locations.
Profile-specific files like application-dev.properties load only when that profile is active.
System properties and environment variables are useful for containerized apps.
Spring Boot loads configuration from many places in a clear priority order.
Command line arguments have the highest priority and override other sources.
Understanding this order helps you control app behavior easily.
Practice
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 BQuick Check:
Highest priority = Command line arguments [OK]
- Thinking application.properties has highest priority
- Confusing environment variables with command line args
- Assuming default properties override others
application.properties file?Solution
Step 1: Recall Spring Boot property syntax
Spring Boot uses dot notation inapplication.propertiesfiles, likeserver.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 AQuick Check:
Properties use dot notation = server.port=8080 [OK]
- Using underscores instead of dots
- Using colon instead of equals sign
- Using camelCase instead of dot notation
application.properties sets server.port=8080Environment variable
SERVER_PORT=9090Command line argument
--server.port=7070What port will the application run on?
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 AQuick Check:
Command line args override others = 7070 [OK]
- Choosing environment variable value instead of command line
- Assuming properties file overrides environment variables
- Forgetting default port fallback
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?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 DQuick Check:
Incorrect syntax means command line ignored [OK]
- Assuming properties file always overrides command line
- Thinking environment variables override command line
- Believing Spring Boot ignores command line args by default
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?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 CQuick Check:
Lowest to highest priority = Default properties < application.properties < Environment variables < Command line arguments [OK]
- Mixing order of environment variables and properties file
- Placing default properties above application.properties
- Reversing command line argument priority
Ready to prove it?
Pass the test and this pattern moves unknown
