Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring "spring.profiles.include" overrides

Tags:

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.

like image 210
Daniel Szymatowicz Avatar asked Nov 18 '17 16:11

Daniel Szymatowicz


People also ask

Are spring profiles deprecated?

profiles. active cannot be used in a specific environment, i.e. application-<profile>. yaml , nor can it be used in multiple documents separated by --- . In a nutshell, you can no longer use spring.

How do you define beans for a specific profile?

Bean definition profiles are a mechanism by which application context is configured differently for different environments. You group bean definitions under named profiles in XML or using annotation and activate one or more profiles in each environment.

How do I add multiple accounts to spring boot?

The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.


1 Answers

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

like image 92
Indra Basak Avatar answered Jan 01 '23 20:01

Indra Basak