Java and Spring Level 1 This level · 80 of 105
Easy java spring

What happens if application.yml and application.properties both exist?

The 30 second answer

Spring Boot loads both files rather than choosing just one. If the same property appears in both at the same location, application.properties takes precedence over application.yml. Properties that appear in only one file still contribute normally. That rule doesn't override the broader precedence rules for profiles or other configuration sources. I'd normally use one format consistently, as the documentation recommends, because mixing formats makes overrides harder to follow.

Could you say this in an interview?

What the docs say: "If you have configuration files with both .properties and YAML format in the same location, .properties takes precedence."

Example:

# application.properties
server.port=9000
# application.yml
server:
  port: 8081
spring:
  application:
    name: orders

Result: server.port=9000 (from .properties) and spring.application.name=orders (only in YAML). Both files are active; only the overlapping key has a winner.

Why it works this way in the code. Spring Boot registers its file loaders in this order:

org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader

spring.factories L8-L10. When building the references for one config name, the resolver goes through the loaders and their extensions and adds each with addFirst:

for (PropertySourceLoader propertySourceLoader : this.propertySourceLoaders) {
    for (String extension : propertySourceLoader.getFileExtensions()) {
        StandardConfigDataReference reference = new StandardConfigDataReference(configDataLocation, directory,
                directory + name, profile, extension, propertySourceLoader);
        if (!references.contains(reference)) {
            references.addFirst(reference);
        }
    }
}

StandardConfigDataLocationResolver.java L188-L199. The docs state the outcome: .properties takes precedence.

Location beats format. The format rule only applies within the same location. An application.yml in ./config/ outside the jar still beats an application.properties inside the jar, because location priority is checked first. Profile files also beat non-profile files regardless of format.

Other format differences:

  • YAML supports multi-document files with ---; .properties uses #---.
  • YAML files cannot be loaded with @PropertySource; .properties can.
  • YAML lists map to my.list[0], my.list[1] keys.

Editor's note: pick one format per project. Two formats at once makes it easy to change a value in the wrong file and wonder why nothing happens.

Sources (2)
  1. Spring Boot reference: External application properties (.properties takes precedence)docs.spring.io
  2. Spring Boot reference: Working with YAMLdocs.spring.io
Previous
Next