Spring Property Precedence Rules Explained | Generated by AI

Home 2025.09

In Spring, when you have duplicate property keys across your configuration sources (e.g., application.properties, application.yml, environment variables, command-line arguments, etc.), the last one with the highest precedence wins.

Here’s how it works in detail:

1. Property Source Ordering

Spring Boot applies a well-defined property source order. For example:

If the same property key appears in multiple sources, Spring picks the one from the highest-priority source.

2. Inside the Same File

If you have the same property key repeated in a single file (like application.properties), Spring uses the last declared occurrence. Example (application.properties):

server.port=8080
server.port=9090

Here, server.port=9090 takes effect.

3. Across Multiple Files

If you define the same property in application.properties and application-dev.properties, and you activate the dev profile, then the value from application-dev.properties will override the default one.

4. With @PropertySource

If you load multiple property files via @PropertySource, the order of declaration matters. The later one can override earlier values unless you mark it with ignoreResourceNotFound or explicitly control precedence.


Summary:

Do you want me to also show you the exact Spring Boot property precedence hierarchy (from highest to lowest)? That gives a very clear picture of where overrides happen.


Back Donate