DEV Community

Hunor Vadasz-Perhat
Hunor Vadasz-Perhat

Posted on

spring-017: spring-boot-property-loading-overview

Where does Spring look for application.properties?

Spring Boot searches for application.properties in the following order (highest priority wins):

Image description

🔹 Override with CLI args:

java -jar myapp.jar --spring.config.location=file:/mydir/application.properties
Enter fullscreen mode Exit fullscreen mode

How does Spring Boot load properties?

Spring Boot internally uses ConfigFileApplicationListener to load properties.

Key components:

  • ConfigFileApplicationListener → Detects and loads application.properties
  • PropertySourceLoader → Converts properties into key-value pairs
  • YamlPropertySourceLoader → Handles application.yml

🔹 Spring Boot loads properties like this:

new ConfigFileApplicationListener().addPropertySources(environment, resourceLoader);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

2️⃣ Log property loading in application.properties:

logging.level.org.springframework.boot.context.config=DEBUG
Enter fullscreen mode Exit fullscreen mode

What if application.properties is missing?

Spring Boot falls back to default values.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay