Spring profiles provide the ability to separate parts of your application's configuration and make them accessible only in certain environments. Any @Component, @Configuration or @ConfigurationProperties can be marked with a @Profile annotation to restrict loading, as shown in the following example:
@Configuration(proxyBeanMethods = false) @Profile("production") public class ProductionConfiguration { // ... }
@Configuration(proxyBeanMethods = false) @Profile("production") class ProductionConfiguration { // ... }
@ConfigurationProperties annotation are registered through the
@EnableConfigurationProperties annotation instead of automatically scanning, the
@Profile annotation must be set to a class marked with the
@Configuration annotation that contains the
@EnableConfigurationProperties annotation. In case
@ConfigurationProperties annotations are scanned, the
@Profile annotation can be specified for the class itself, marked with the
@ConfigurationProperties annotation.
You can use the spring.profiles.active property for Environment to specify which profiles are active. The property can be set in any of the ways described earlier in this chapter. For example, you can include it as part of the application.properties file, as shown in the following example:
spring.profiles.active=dev,hsqldb
spring:
profiles:
active: "dev,hsqldb"
You can also set it on the command line using the following switch: --spring.profiles.active=dev,hsqldb.
If no profile is active, the default profile will be activated. The default profile name is default and can be fine-tuned using the spring.profiles.default property for Environment, as shown in the following example:
spring.profiles.default=none
spring:
profiles:
default: "none"
spring.profiles.active and spring.profiles.default can only be used in non-profile documents. This means that they cannot be included in profile-specific files or documents activated using spring.config.activate.on-profile.
For example, the configuration of the second document is invalid:
# this document is valid
spring.profiles.active=prod
#---
# this document is invalid
spring.config.activate.on-profile=prod
spring.profiles.active=metrics
# this document is valid
spring:
profiles:
active: "prod"
---
# this document is invalid
spring:
config:
activate:
on-profile: "prod"
profiles:
active: "metrics"
Adding active profiles
The property spring.profiles.active follows the same ordering rules as other properties: The one with the highest PropertySource has the highest priority. This means that you can set active profiles in application.properties and then replace them using a command line switch.
Sometimes it is useful to have properties that adding to active profiles rather than replacing them. The spring.profiles.include property can be used to add active profiles on top of those enabled by the spring.profiles.active property. The SpringApplication entry point also has a Java API for installing additional profiles. See the setAdditionalProfiles() method in the section on SpringApplication.
For example, if an application runs with the following properties, the public and local profiles will be activated, even if it is run using the --spring.profiles.active switch:
spring.profiles.include[0]=common
spring.profiles.include[1]=local
spring:
profiles:
include:
- "common"
- "local"
spring.profiles.active,
spring.profiles.include can only be used in documents, not related to the profile. This means that it cannot be included in profile-specific files or documents activated using
spring.config.activate.on-profile.
Profile groups
Sometimes the profiles that are defined and used in an application are too fine-grained and become cumbersome to use. For example, there may be profiles proddb and prodmq, which are used to independently enable database and messaging functionality.
The Spring Boot feature helps with this, which allows you to define groups of profiles. A profile group allows you to define a logical name for an associated profile group.
For example, we can create a production group consisting of our proddb and prodmq profiles.
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
spring:
profiles:
group:
production:
- "proddb"
- "prodmq"
The application can now be launched using --spring.profiles.active=production to activate the production, proddb and prodmq profiles at a time.
Program installation of profiles
You can set active profiles programmatically by calling SpringApplication.setAdditionalProfiles(…) before running the application. You can also activate profiles using the ConfigurableEnvironment interface from Spring.
Configuration files associated with a specific profile
Profile-specific options for both application.properties (or application.yml) and files referenced through the @ConfigurationProperties are counted as files and downloaded.
GO TO FULL VERSION