spring yml file for specific environment
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
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, 2022Comments
-
Amar Dev almost 2 years
I have 3
yml
files namelyapplication-default.yml
-> default properties, should be available in all profilesapplication-dev.yml
-> properties only for dev profileapplication-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 theapplication-dev.yml
specific properties. But I cant get the properties defined in theapplication-default.yml
files. Following is myapplication-dev.yml
file:Spring: profiles: include: default spring.profiles: dev prop: key:value
-
Amar Dev about 8 yearsThanks 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 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 over 7 years@Arunprasad Yes, but then you have to load it manually using
@PropertySource
annotation -
kubanczyk over 6 years@Arunprasad To change
application.yml
tomyapp.yml
(keeping alsomyapp-{profile}.yml
) you need tojava -Dspring.config.name=myapp ...
But bear in mind: the jar file probably containsapplication.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 about 6 yearsJust curious, what changes compare to previous setup. This look similar to what you have in your application-dev.yml previously.
-
Ev0oD almost 3 yearsYou probably meant application-default.yml? Or did you use include: common?
-
Ev0oD almost 3 yearsThe 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 almost 3 yearsWell that's example. Should be
dev, dev-db, test-data...
or so. Just the idea.