Activating profiles in Spring Boot
If you're using Spring Boot, profiles can be activated in the following ways:
spring.profiles.active
propertyspring.profiles.include
propertyenvironment.setActiveProfiles
SpringApplication#additionalProfiles
orSpringApplicationBuilder.profiles()
The spring.profiles.active
property is processed only once from the property source with the highest precendence.
Caveats:
-
If a property source is added after the config files have been processed,
spring.profiles.active
from that property source will not be picked up if the property is specified in another property source, even if the property source added later has higher precendence. -
If
spring.profiles.active
is present in a profile-specific file, it will not be picked up if the simple config file (eg, application.properties) also has that property, even though the profile-specific file has a higher precendence. -
spring.profiles.active
from the default property source should not be considered when processing config files. Profiles activated via default properties will only be added to the environment if no other property sources have done so. See gh-15445
The spring.profiles.include
property is additive. Profiles added via this property are always added to the environment provided that the property source is available before the config files are processed.
Behavior:
-
Profiles added via
spring.profiles.include
will have lower precedence than profiles added viaspring.profiles.active
unless they are included because of a profile being active. For example, ifspring.profiles.active=bar
andapplication-bar.properties
containsspring.profiles.include=baz
, the baz profile will have higher precedence thanbar
. However, ifspring.profiles.active=bar
andapplication.properties
containsspring.profiles.include=baz
,bar
will have higher precendence. -
spring.profiles.include
have a cascading effect. If a higher precedence property source containsspring.profiles.include=bar
andapplication-bar.properties
containsspring.profiles.include=baz
,baz
will have higher precendence. However if the included profiles are unrelated to the other being included, the profile from the higher property source will take precedence. (gh-16023 might affect this behavior) -
Profiles from
spring.profiles.include
from the default property source should be added after profiles from all the property sources. They should have the lowest precedence, even after thespring.profiles.active
from the default property source. See gh-15445
Caveats:
Profiles added via spring.profiles.include
in a property source that gets added to the environment after the config files have been processed will not take effect.
Profiles added via environment.setActiveProfiles
and SpringApplication#additionalProfiles
. Currently, their behavior is similar to profiles added via spring.profiles.include
in a property source with higher precedence than the config files.