I have 3 yml
files namely
application-default.yml
-> default properties, should be available
in all profilesapplication-dev.yml
-> properties only for dev
profileapplication-prod.yml
-> properties only for prod profileWhen 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
Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.
PY-yaml library doesn't resolve environment variables by default. You need to define an implicit resolver that will find the regex that defines an environment variable and execute a function to resolve it. You can do it through yaml.
Note: You can refer to multiple . yml files this way if you want. You need additional . yml files and Config classes like below example.
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.
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!!!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With