2020-02-07

Spring - Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it be
available only in certain environments. Any @Component, @Configuration or @ConfigurationProperties
can be marked with @Profile to limit when it is loaded, as shown in the following example:
@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {
// ...
}


If @ConfigurationProperties beans are registered via
@EnableConfigurationProperties instead of automatic scanning, the @Profile
annotation needs to be specified on the @Configuration class that has the
@EnableConfigurationProperties annotation. In the case where
@ConfigurationProperties are scanned, @Profile can be specified on the
@ConfigurationProperties class itself.

You can use a configprop:spring.profiles.active[] Environment property to specify which profiles are
active. You can specify the property in any of the ways described earlier in this chapter. For
example, you could include it in your application.properties, as shown in the following example:
spring.profiles.active=dev,hsqldb
You could also specify it on the command line by using the following switch:
--spring.profiles.active=dev,hsqldb.

Adding Active Profiles

The configprop:spring.profiles.active[] property follows the same ordering rules as other
properties: The highest PropertySource wins. This means that you can specify active profiles in
application.properties and then replace them by using the command line switch.
Sometimes, it is useful to have profil-specific properties that add to the active profiles rather than
replace them. The configprop:spring.profiles.include[] property can be used to unconditionally add
active profiles. The SpringApplication entry point also has a Java API for setting additional profiles
(that is, on top of those activated by the configprop:spring.profiles.active[] property). See the
setAdditionalProfiles() method in SpringApplication.
For example, when an application with the following properties is run by using the switch,
--spring.profiles.active=prod, the proddb and prodmq profiles are also activated:

---
my.property: fromyamlfile
---
spring.profiles: prod
spring.profiles.include:
- proddb
- prodmq

Remember that the spring.profiles property can be defined in a YAML document
to determine when this particular document is included in the configuration. See
Change Configuration Depending on the Environment for more details.

Programmatically Setting Profiles

You can programmatically set active profiles by calling SpringApplication.setAdditionalProfiles(…) before your application runs. It is also possible to activate profiles by using Spring’s
ConfigurableEnvironment interface.

Profile-specific Configuration Files

Profile-specific variants of both application.properties (or application.yml) and files referenced
through @ConfigurationProperties are considered as files and loaded. See "Profile-specific
Properties" for details.


No comments:

Post a Comment