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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

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

Okay