Spring "spring.profiles.include" overrides

Daniel Szymatowicz picture Daniel Szymatowicz · Nov 18, 2017 · Viewed 27.5k times · Source

My intention is to have two profiles in a spring boot application - development and production one. Development profile is meant just to override some variables of production profile (like in-memory database instead of database in the cloud). As I expect some changes to be done to production profile in the future, duplicating variables in development profile doesn't seem to be a solution.

So, in Spring Reference I read that spring.profiles.include is supposed to only add properties from referenced profile.

Sometimes, it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles.

However, from what I've checked it rather overrides it. So, when having two profiles foo and bar, in separate yaml files:

application-foo.yaml:

myproperty: 44

application-bar.yaml:

spring:
  profiles:
    include: foo
    active: bar,foo
myproperty: 55

And setting -Dspring.profiles.active=bar variable in IDE, the runtime value of myproperty is 44. That means that bar, is overriden with foo which was supposed to only add properties, but not to override them. When starting the application, I get:

The following profiles are active: foo,bar

I added spring.profiles.active=bar to application-bar.yaml as suggested by this answer, in another question, but it has no effect - there is no difference when property is there or not (I also tried using dash listing instead of comma separated values).

My question is, is it how it is supposed to work (then Spring Reference is misleading)? If so, are there any solutions for that?

Adding a link to the application source code on a github.

Answer

Indra Basak picture Indra Basak · Nov 19, 2017

We implemented the Spring active profiles in a slightly different way. Let's say the default properties file, application.yml, contains all default values which is same in both production and development environments.

Create separate properties for production and development files named application-prd.yml and application-dev.yml respectively. These files may contain extra properties or override some of the default properties.

During application startup, we pass the spring.profiles.active as an environment variable. For example,

-Dspring.profiles.active=prd

will pick up application-prd.yml along with application.yml

or

-Dspring.profiles.active=dev

will pick up application-dev.yml along with application.yml