Home > database >  Running Spring Boot App with Multiple Spring Profiles
Running Spring Boot App with Multiple Spring Profiles

Time:09-25

I have a Spring Boot application (running in Docker, but I don't think that's relevant) that I start as follows:

java -Dspring.profiles.active=development,docker -jar /app.jar

My Spring Boot development profile includes an entry:

service.baseurl: http://foo

My Spring Boot docker profile includes an entry:

service.baseurl: http://bar

My understanding of how Spring profiles work is that values are picked up from the LAST value found when there are multiple profiles (see https://stackoverflow.com/a/51565684/2117355). Based on this, I would expect that my application loads http://bar as the value for service.baseurl. This is in fact what happens on my development machine.

However, on my teammate's machine, the value loaded for service.baseurl is http://foo.

This one is a real head-scratcher. Could there be some setting, or version of something, on my teammate's machine that is causing her to get different results here?

For what it's worth, we are both running:

  • Java JDK 16.0.2
  • Spring Boot 2.5.4
  • Gradle 7.2
  • Docker 20.10.8

CodePudding user response:

If I'm not mistaken, the main file that spring load is application.yaml, you can override the value from this file in other profiles, but other profiles can't override the value that was declared in another profile.

In you case, you can declare all development configuration in application.yaml and in application-docker.yaml override the specific properties. Then, when you want to run your service in docker just set docker profile.

CodePudding user response:

Problem solved. For development purposes, devs on this project create their own application-development.properties files which are not committed to Git. I was under the assumption that everyone was creating this file under src/main/resources. But the developer who experienced different behavior had placed this file in the root directory of the project. This causes Spring to load the properties in a different fashion (which caused only one profile to be picked up on startup.

Be careful where you put your properties files.

  • Related