Where does Spring look for application.properties
?
Spring Boot searches for application.properties
in the following order (highest priority wins):
🔹 Override with CLI args:
java -jar myapp.jar --spring.config.location=file:/mydir/application.properties
How does Spring Boot load properties?
Spring Boot internally uses ConfigFileApplicationListener
to load properties.
Key components:
-
ConfigFileApplicationListener
→ Detects and loadsapplication.properties
-
PropertySourceLoader
→ Converts properties into key-value pairs -
YamlPropertySourceLoader
→ Handlesapplication.yml
🔹 Spring Boot loads properties like this:
new ConfigFileApplicationListener().addPropertySources(environment, resourceLoader);
What happens when a property is accessed?
When environment.getProperty("server.port")
is called, Spring searches in this order:
1️⃣ Command-line arguments (--server.port=9090
)
2️⃣ Environment variables (export SERVER_PORT=8081
)
3️⃣ System properties (-Dserver.port=7070
)
4️⃣ application.properties (server.port=6060
)
5️⃣ Default value (server.port=8080
)
🔹 Spring stops searching once a value is found!
How to debug property loading?
1️⃣ Enable debug logs:
java -jar myapp.jar --debug
2️⃣ Log property loading in application.properties
:
logging.level.org.springframework.boot.context.config=DEBUG
What if application.properties
is missing?
Spring Boot falls back to default values.
Top comments (0)