Affects: Spring Boot 2.4.4


Minimum viable reproducible example can be found here https://github.com/alextadams88/spring-issue-26774

I am running a @SpringBootTest with @ActiveProfiles to define which profile to use during the test. I have a property in the profile specified by @ActiveProfiles which is set to false.

Simultaneously, I have a jar in my dependency tree which includes an application.yml file. In this application.yml file, the same property is set to true. I am finding that, when running the test, Spring is using the property from the application.yml file in the jar ("true"), rather than the property from the profile defined in my @ActiveProfiles ("false").

Per the documentation in https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config, this is not the expected behavior:

Config data files are considered in the following order:

1.Application properties packaged inside your jar (application.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.Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).

I should expect that my properties defined in @ActiveProfiles would fall under 1, and the configuration file in the dependency jar to fall under 3, so the property in my @ActiveProfile should be used.

Test class:

Spring Properties resolving in incorrect order during test when using @ActiveProfiles and properties file in dependent jar

My file application-it.yml contains the property data-warehouse: enabled: false

My classpath contains a jar file kafka-producer-1.0.9-rc.1e827024.jar, which contains application.yml with the property data-warehouse: enabled: false

When debugging, I find that the property with the incorrect value is being used:

Spring Properties resolving in incorrect order during test when using @ActiveProfiles and properties file in dependent jar

The origin for this property is from the kafka-producer-1.0.9-rc.1e827024.jar file:

Spring Properties resolving in incorrect order during test when using @ActiveProfiles and properties file in dependent jar

However, I do see that my application-it.yml file is indeed being picked up, simply just not prioritized:

Spring Properties resolving in incorrect order during test when using @ActiveProfiles and properties file in dependent jar

I am unsure if the order of the property sources in this list is an indication of the resolution order, but if so, this demonstrates that the wrong resolution order is being used.

Minimum viable reproducible example can be found here https://github.com/alextadams88/spring-issue-26774

EDIT: After playing around with my minimum example for a bit, it is revealed that the issue disappears if the configuration in the dependent library is moved out of the "resources/config/" folder, and directly into the "resources/" folder. Looking through the documentation a bit more, I find the following:

Spring Boot will automatically find and load application.properties and application.yaml files from the following locations when your application starts: 1. The classpath root 2. The classpath /config package 3. The current directory 4. The /config subdirectory in the current directory 5. Immediate child directories of the /config subdirectory

The list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as PropertySources to the Spring Environment.

As my configuration is the in the /config package, this explains the issue. Closing this ticket.

Comment From: alextadams88

After playing around with my minimum example for a bit, it is revealed that the issue disappears if the configuration in the dependent library is moved out of the "resources/config/" folder, and directly into the "resources/" folder. Looking through the documentation a bit more, I find the following:

Spring Boot will automatically find and load application.properties and application.yaml files from the following locations when your application starts:

  1. The classpath root
  2. The classpath /config package
  3. The current directory
  4. The /config subdirectory in the current directory
  5. Immediate child directories of the /config subdirectory

The list is ordered by precedence (with values from lower items overriding earlier ones). Documents from the loaded files are added as PropertySources to the Spring Environment.

As my configuration is the in the /config package, this explains the issue. Closing this ticket.