spring yml file for specific environment

28,302

Solution 1

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!!!

Solution 2

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.

Solution 3

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

Share:
28,302
Amar Dev
Author by

Amar Dev

A learner who craves to learn new things. I am always trying to improve my abilities as a software developer. In my free time I watch movies and eat a lot of food!!!!!

Updated on August 01, 2022

Comments

  • Amar Dev
    Amar Dev almost 2 years

    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
    
  • Amar Dev
    Amar Dev about 8 years
    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.
  • Arunprasad
    Arunprasad over 7 years
    @Daniel Olszewski is there any option to name the yml or properties file other than application . say like datasource.yml
  • Daniel Olszewski
    Daniel Olszewski over 7 years
    @Arunprasad Yes, but then you have to load it manually using @PropertySource annotation
  • kubanczyk
    kubanczyk over 6 years
    @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.
  • Fadhlie Ikram
    Fadhlie Ikram about 6 years
    Just curious, what changes compare to previous setup. This look similar to what you have in your application-dev.yml previously.
  • Ev0oD
    Ev0oD almost 3 years
    You probably meant application-default.yml? Or did you use include: common?
  • Ev0oD
    Ev0oD almost 3 years
    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.
  • WesternGun
    WesternGun almost 3 years
    Well that's example. Should be dev, dev-db, test-data... or so. Just the idea.