Lukas Forst
Lukas Forst

Reputation: 842

Spring Boot does not correctly load profiles from application.yml

I have a Spring Boot project and I'm injecting values to application.yml via the application.properties (this design makes sense for me since there are a lot of microservices and application.properties is actually a symlink) like this:

in application.properties:

PROPERTY_YAML=something

and then in application.yml:

app:
  property:
    yaml: ${PROPERTY_YAML}

This works just fine and when I'm accessing it via Environment.getProperty or via @Value everything works as expected. However, when I try to set the Spring Profile the same way, it does not work. Setting SPR_PROFILE=my_profile in application.properties and application.yml as

spring:
  profiles:
    active: ${SPR_PROFILE}

results in the inconsistency in the spring when the Environment.activeProfiles returns ${SPR_PROFILE} (and spring is running under this profile) and the property Environment.getProperty("spring.profiles.active") returns my_profile.

Is this expected behavior? Why is that? Does spring load profiles before actual parsing/replacing the placeholders?

My current workaround is to set spring.profiles.active=my_profile in application.properties but I was wondering, why is spring behaving that way.

Upvotes: 1

Views: 2866

Answers (1)

MartinBG
MartinBG

Reputation: 1648

I cannot answer the specific question, but I wouldn't use both application.properties and application.yml as according to Spring documentation both are with the same priority and there's no guarantee which loads first:

  1. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).

  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).

  3. Application properties outside of your packaged jar (application.properties and YAML variants).

  4. Application properties packaged inside your jar (application.properties and YAML variants).

I would put all shared properties in a custom *.properties file - preferable with a profile (ex. applicaion-dev.properties) and activate that profile using one of Spring's various ways, which should guarantee loading before default properties.

Upvotes: 3

Related Questions