55

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

Scott C.
  • 3,672
  • 4
  • 18
  • 20

3 Answers3

64

I can answer my own question, as it just works as you would expect. The application.yml file and the appropriate application-${profile}.properties both get loaded and merged into the environment. Spring boot just makes this work naturally.

armandino
  • 17,625
  • 17
  • 69
  • 81
Scott C.
  • 3,672
  • 4
  • 18
  • 20
  • 3
    I would suggest the reference documentation be updated to clarify if this answer is correct. Right now the language in the guide makes it sound like its either or but not both. Let me know if I should open up a JIRA ticket to get this updated. – Scott C. Aug 12 '14 at 14:56
  • I concur, this is why I have decided to Google the matter and hence arrived here :) – theadam Feb 02 '15 at 00:24
  • @ScottC. is correct .both application.yml and application-{profile}.properties are getting merged. – Arunprasad Jan 20 '17 at 07:31
  • 5
    What about if you have both `application.yml` and `application.properties`? – OrangeDog Dec 06 '18 at 11:43
  • 5
    u can keep both files but the properties files loads later and can overwrite the values if same is defined in the yml file as well. – Gautam Tyagi Jan 23 '20 at 11:16
  • 4
    @OrangeDog `application.properties` > `application.yml`, meaning yml loads first and properties later. Any `application-profile.properties` will be added even later. This is a nice way to override values in `application.properties` IMO. – Avec Dec 22 '21 at 10:25
49

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.
Touheed Khan
  • 2,149
  • 16
  • 26
Gowri Sundar
  • 669
  • 1
  • 7
  • 15
  • 2
    Your sentence "first application.yml will be loaded **later**, application.properties will be loaded." should be "first application.yml will be loaded, application.properties will be loaded **later**.", right? – S. Doe Jan 19 '22 at 12:27
  • Yes you are right – Gowri Sundar Feb 02 '22 at 01:33
9

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}

Akshay Pethani
  • 2,390
  • 2
  • 29
  • 42
  • 3
    This is only correct in order of priority but not in order of what is loaded first. Any application-profile.properties is loaded after application.properties. This is how you override values. Also yml is read before properties if you so happens to have both. Example application-profile.properties > application.properties > application.yml, where > (greater than) means left side is greater than right side. – Avec Dec 22 '21 at 10:22