Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 532
user2057006 Avatar asked Aug 31 '25 22:08

user2057006


1 Answers

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.

like image 175
Paulo Merson Avatar answered Sep 03 '25 15:09

Paulo Merson