'Does spring boot support using both properties and yml files at the same time?

I have a spring boot application and I want to use both a yml file for my application properties and also a plain application-${profile}.properties file set to configure my application.

So my question is can this be done and if so, how do you configure spring boot to look for both the yml file and the properties and merge them into one set per environment?

As to why I want/need to use both, it is because I like the flexibility and ease of use of yml files but an internal component (for encryption) requires using the properties file set.

I did see this point being made YAML files can’t be loaded via the @PropertySource annotation

but nothing stating whether both can be used together.

Please provide detailed configuration (XML or Java config) on how to get this working.

TIA,

Scott



Solution 1:[1]

Yes You can use both at same time in same project.

  • When you use both YML and properties at same time, say for example
    application.yml and application.properties at same time in same
    project, first application.yml will be loaded later
    application.properties will be loaded
    .
  • Important point to be noted is that if application.yml and application.properties have same keys for example in application.yml has spring.app.name = testYML and application.properties has spring.app.name = testProperties at same time in same project, then application.yml value will be overwritten by application.properties value since it is loading at last.
  • And the value in spring.app.name = testProperties.

Solution 2:[2]

Yes, you can run both without doing any configuration.

In Spring Boot, it picks .properties or .yaml files in the following sequences :

  1. application-{profile}.{properties|yml}

  2. application.{properties|yml}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gowri Sundar
Solution 2