I would like to be able to do something like this:
public static void main(String[] args) {
System.setProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "mongo");
var application = new SpringApplication(Application.class);
application.setAdditionalProfiles("default-values");
application.run(args);
}
My expectation was that this comes up with mongo,default-values but it comes up with default-values only.
This makes default profiles & additional profiles unusable together.
(I guess #24688 could offer an overall nicer solution for my initial problem, but until then...)
Comment From: nosan
I believe this is the correct behavior. Default profiles are used only when no active profiles are specified. In your example, you added default-values as an additional active profile, so only default-values is considered active.
You can use ACTIVE_PROFILES_PROPERTY_NAME system property instead of DEFAULT_PROFILES_PROPERTY_NAME
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "mongo");
com.example.demo.DemoApplication : The following 2 profiles are active: "default-values", "mongo"
Comment From: mhalbritter
This is correct. Documentation for that is here: https://docs.spring.io/spring-framework/reference/core/beans/environment.html#beans-definition-profiles-default
The default profile represents the profile that is enabled if no profile is active.
Comment From: david0
I was thinking of additionalProfiles as something that should always be active on top of the rest.
I cannot use active profiles, since we set spring_profiles_active via env in the CI and via YAML in production to disable some development-only profiles.
Comment From: philwebb
I was thinking of additionalProfiles as something that should always be active on top of the rest.
They're added on top of active/included profiles but not added on top of default profiles. Perhaps you might be able to use spring.profiles.include (https://docs.spring.io/spring-boot/reference/features/profiles.html#features.profiles.adding-active-profiles).
Failing that, you could use an EnvironmentPostProcessor ordered behind ours that sets the profiles directly on ConfigurableEnvironment.
Comment From: david0
spring.profile.include will also override spring.profiles.default, exactly as setAdditionalProfiles does.
Yes good hint, maybe I should implement EnvironmentPostProcessor, feels a little bit wired to need that for such a simple case, but maybe it is the easiest right now.