user2057006
user2057006

Reputation: 647

Override property value from multiple spring active profile

I have a spring boot application that has application.yml.

Contents of application.yml:

spring:
  profiles:
    active: default,private
integrations:
  ecom:
    api-url: http://localhost:8080/com

Contents of application-private.yml:

integrations:
  ecom:
    api-url: http://testenv:8080/com

As per my understanding, integrations:ecom:api-url are getting loaded from application-private.yml even though the default profile also has same property.

If two profiles are active, will the property be loaded and used in the order the profiles were specified?

My order:

-Dspring.profiles.active="default,private"

Thanks in Advance.

Upvotes: 4

Views: 10512

Answers (3)

Paulo Merson
Paulo Merson

Reputation: 14457

For your example, the following is the order of precedence in which Spring will get the value of the property (highest to lowest priority):

  1. application-private.yml provided outside your jar file (for example, via spring-cloud-config)
  2. application.yml provided outside your jar file (application.yml is equivalent to application-default.yml)
  3. application-private.yml provided inside your jar file
  4. application.yml provided inside your jar file

So, if you have application-private.yml and application.yml inside the jar file, properties in the former override properties in the latter.

However, if application-private.yml is inside the jar but application.yml is outside, the latter will override the former.

See official documentation about external property precedence.

Upvotes: 9

Anish Panthi
Anish Panthi

Reputation: 1022

Yes, configuration files will be loaded as per the series how you determine/define the profiles. Here is how it works with example:

  1. You have application.yml and you have defined your url as: "https://anish.com" Now from same YML configuration, you are loading profile as "prod", so it tries to load "application-prod.yml".

  2. In your "application-prod.yml" file, you have your url mentioned as "https://panthi.com", then in this case, your url value will be overridden by "https://panthi.com".

So, make sure you are trying to match the URLs based on the environment that you want to deploy. If you are loading multiple profiles in production, then make sure that the last profile has everything that you need or you make sure you are not overriding any properties in any of the profiles (unless required)

Upvotes: 1

Ajay Kumar S
Ajay Kumar S

Reputation: 31

In this case, all properties in application.yml will be loaded first and then application-private.yml will be loaded based on the profile, hence overriding your property from application.yml

Upvotes: 2

Related Questions