Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring yml file for specific environment

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
like image 356
Amar Dev Avatar asked May 04 '16 16:05

Amar Dev


People also ask

How do I set environment specific properties in spring boot?

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.

Does YAML support environment variables?

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.

Can we have multiple YAML files in spring boot?

Note: You can refer to multiple . yml files this way if you want. You need additional . yml files and Config classes like below example.


3 Answers

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.

like image 163
Daniel Olszewski Avatar answered Sep 19 '22 05:09

Daniel Olszewski


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

like image 21
Amar Dev Avatar answered Sep 21 '22 05:09

Amar Dev


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

like image 36
WesternGun Avatar answered Sep 20 '22 05:09

WesternGun