8000 SpringApplication additional profiles and active profiles ordering changed with Spring Boot 2.4 · Issue #26189 · spring-projects/spring-boot · GitHub
[go: up one dir, main page]

Skip to content
SpringApplication additional profiles and active profiles ordering changed with Spring Boot 2.4 #26189
@mzeijen

Description

@mzeijen

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()));
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0