Skip to content

Instantly share code, notes, and snippets.

@mbhave
Last active February 23, 2019 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbhave/9444f0bbdfefa67ad2395325ac27ea37 to your computer and use it in GitHub Desktop.
Save mbhave/9444f0bbdfefa67ad2395325ac27ea37 to your computer and use it in GitHub Desktop.
Profile activation in Spring Boot

Activating profiles in Spring Boot

If you're using Spring Boot, profiles can be activated in the following ways:

  • spring.profiles.active property
  • spring.profiles.include property
  • environment.setActiveProfiles
  • SpringApplication#additionalProfiles or SpringApplicationBuilder.profiles()

spring.profiles.active property

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

spring.profiles.include property

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 via spring.profiles.active unless they are included because of a profile being active. For example, if spring.profiles.active=bar and application-bar.properties contains spring.profiles.include=baz, the baz profile will have higher precedence than bar. However, if spring.profiles.active=bar and application.properties contains spring.profiles.include=baz, bar will have higher precendence.

  • spring.profiles.include have a cascading effect. If a higher precedence property source contains spring.profiles.include=bar and application-bar.properties contains spring.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 the spring.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 programmatically

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment