question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SpringApplication additional profiles and active profiles ordering changed with Spring Boot 2.4

See original GitHub issue

Spring Boot version: 2.4

After upgrading to Spring Boot 2.4 from Spring Boot 2.3 I noticed that the ordering has changed of how SpringApplication.additionalProfiles are merged with the ConfigurableEnvironment.getActiveProfiles().

With Spring Boot 2.3 the ordering is: additional profiles, active profiles With Spring Boot 2.4 the ordering is: active profiles, additional profiles

When running the test application below with Spring Boot 2.3 and Spring Boot 2.4 you can see this easily in the logging:

Spring Boot 2.3.10.RELEASE profiles ordering: additional, active Spring Boot 2.4.5 profiles ordering: active, additional

This ordering change also affects the @ActiveProfiles test annotation. The profiles activated via the annotation now also have a lesser priority then the additional profiles (if you are wondering how we are setting additional profiles in combination with running tests, this is because we are actually using a ApplicationStartingEvent ApplicationListener, registered via the spring.factories to call the SpringApplication.setAdditionalProfiles() with an environment specific profile).

This change has the effect that our application-{profile}.yml property sources are now getting different precedence, which caused issues for us when running tests, because property values where not being overridden in the expected order anymore.

I wonder if this change in ordering was an intentional one or that it is a mistake. I couldn’t find any remarks regarding the change in the changelog. I have also read the https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Config-Data-Migration-Guide but I couldn’t distill this information from that document as well. If the ordering change is intentional then I recommend adding a note in the migration guide.

Test code:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class AdditionalProfilesReproductionApplication implements CommandLineRunner {

    public static void main(String[] args) {
        System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "active");

        SpringApplication app = new SpringApplication(AdditionalProfilesReproductionApplication.class);
        app.setAdditionalProfiles("additional");
        app.run(args);
    }

    @Autowired
    Environment environment;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Profiles: " + String.join(", ", environment.getActiveProfiles()));
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mzeijencommented, Apr 22, 2021

@nguyensach I tried activating legacy processing (by setting spring.config.use-legacy-processing to true in the application.properties or via the System properties before starting the application) but that didn’t restore the old behaviour.

From what I can see the ordering of the additional profiles and the active profiles happens in the SpringApplication, and I don’t see that the legacy processing has any influence on it.

In Spring Boot 2.3 it is done in the configureProfiles method: https://github.com/spring-projects/spring-boot/blob/32e846da2f60ad1da9a67f3ed5bcdab61a6027f6/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L519-L523

In Spring Boot 2.4 it is done in the configureAdditionalProfiles method: https://github.com/spring-projects/spring-boot/blob/7caf238a90fbb67a23666ba6ed3384011af01378/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L560-L568

The difference is that in Spring Boot 2.3 the list of profiles is created with this.additionalProfiles as initial fill where in Spring Boot 2.4 the environment.getActiveProfiles() is used as initial fill.

1reaction
mzeijencommented, Apr 23, 2021

@nguyensach Thanks for clearing that up. Can you also make sure the intentional ordering change is documented in the changelog so that it doesn’t surprise others like me? Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Config file processing in Spring Boot 2.4
Profile groups are a new feature in Spring Boot 2.4 that allow you to expand a single profile into multiple sub-profiles. For example,...
Read more >
Including profiles in spring boot 2.4.0 version - Stack Overflow
Documents will be loaded in the order that they're defined. Profiles can no longer be activated from profile specific documents. This change has ......
Read more >
Spring Profiles - Baeldung
How to define and use Profiles in Spring. The 4 ways to enable profiles and their precedence. And a real-world example using profiles....
Read more >
Step-by-Step Guide to Profiles with Spring Boot - HackerNoon
Spring Profiles provide a way to segregate parts of your application configuration and make it available only in certain environments.
Read more >
How to use Spring Profiles - Tutorial with Examples - amitph
The Spring profile is a group of variables and configurations in a Spring application that we can plugin during the application startup. Having...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found