20

I have 3 yml files namely

  • application-default.yml -> default properties, should be available in all profiles
  • application-dev.yml -> properties only for dev profile
  • application-prod.yml -> properties only for prod profile

When I start my boot application by passing the -Dspring.profiles.active=dev,I am able to access the application-dev.yml specific properties. But I cant get the properties defined in the application-default.yml files. Following is my application-dev.yml file:

Spring:
 profiles:
  include: default

spring.profiles: dev

prop:
 key:value
Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
Amar Dev
  • 1,360
  • 2
  • 18
  • 38

3 Answers3

33

TL;DR

Just rename the application-default.yml file to application.yml and will work as you expect.

Explanation

According to the description in the docs, a file called application-{suffix}.yml is activated when you run your application with the profile which name matches with the suffix. In addition, the main application.yml is loaded by default so it's the perfect place to put common properties for all profiles. Alternatively, if you want to keep the name of your file as application-default.yml you can pass two profiles to your Spring Boot application:

-Dspring.profiles.active=default,dev

This way you will activate two profiles and both properties files will be loaded.

Daniel Olszewski
  • 13,995
  • 6
  • 58
  • 65
  • Thanks Daniel for you response, this too should work,I guess, as we are explicitly activating both the profiles. I will try this as well. – Amar Dev May 05 '16 at 05:42
  • @Daniel Olszewski is there any option to name the yml or properties file other than application . say like `datasource.yml` – Arunprasad Jan 24 '17 at 07:03
  • @Arunprasad Yes, but then you have to load it manually using `@PropertySource` annotation – Daniel Olszewski Jan 24 '17 at 07:30
  • 1
    @Arunprasad To change `application.yml` to `myapp.yml` (keeping also `myapp-{profile}.yml`) you need to `java -Dspring.config.name=myapp ...` But bear in mind: the jar file probably contains `application.yml`; it would be ignored now; it's a major change; you cannot know what new settings would appear there with a new jar version. – kubanczyk Oct 26 '17 at 21:42
9

I was able to solve my problem, here is what I did.

Created a file application-common.yml, put the common properties there. Then in the application-{env}.yml files I put this on the top.

spring:
 profiles:
  include: default

Since I dont need to ever load the default profile specifically, this works for me!!!

Amar Dev
  • 1,360
  • 2
  • 18
  • 38
1

What I do is:

Put common settings in application.xml, and in this file add:

spring:
  profiles:
    active: dev, pro, xxx...

all the profiles you want to activate.

So that you just edit this file to switch environment.

Remember that external files procedes, so you can leave another application.xml outside of the WAR to activate dev/pro/... environment instead of editing this file every time. Be sure to check the documentation:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

WesternGun
  • 11,303
  • 6
  • 88
  • 157
  • 1
    The example is strange. You're activating both dev and production environment at the same time? Either I'm missing something or the answer is unnecessarily confusing for people just learning about this. – Ev0oD Jun 01 '21 at 16:55
  • 1
    Well that's example. Should be `dev, dev-db, test-data...` or so. Just the idea. – WesternGun Jun 01 '21 at 17:32