Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring.profiles.active priority for application-{profile}.properties

Tags:

spring

I would like to know the priority of the application-{profile}.properties files, if multiple spring.profiles.active are added.

For example:

  • Let say, I have this spring.profiles.active=profile1,profile2
  • And in my src/main/resources, I have these files application-profile1.properties and application-profile2.properties
  • If both of the files config the same property, but with different values, which one will be the one shown?

I have read this and but it doesn't cover this topics here.

Thanks in advance.

like image 295
Ng Sek Long Avatar asked Apr 26 '19 08:04

Ng Sek Long


People also ask

How many profile can be defined for a spring application?

In the application. properties we have set the local profile to be active. With the SpringApplicationBuilder's profiles method we add two additional profiles.

Which spring boot property sets the profile for an application?

Spring profiles can also be activated via Maven profiles, by specifying the spring. profiles. active configuration property. This command will package the application for prod profile.

How do you find more information about your application environment using spring boot?

To see all properties in your Spring Boot application, enable the Actuator endpoint called env . This enables an HTTP endpoint which shows all the properties of your application's environment. You can do this by setting the property management.


2 Answers

According to the documentation, the last profile specified wins.
In your example : spring.profiles.active=profile1,profile2, the properties of profile2 should overwrite the properties of profile1.

In the point 24.4 Profile-specific Properties of the documentation, you can read :

If several profiles are specified, a last-wins strategy applies. For example, profiles specified by the spring.profiles.active property are added after those configured through the SpringApplication API and therefore take precedence.

like image 57
davidxxx Avatar answered Oct 05 '22 16:10

davidxxx


First Spring loads default profile (application.properties) and then it overrides it with your additional profiles in the same order you listed them. So in your case profile2 values will override everything you defined in profile and application.properties

The simplest way is to write very simple application and just check it:)

like image 30
Planck Constant Avatar answered Oct 05 '22 16:10

Planck Constant