# Spring Boot 2.0.0参考手册_中英文对照_Part IV_25

1242人阅读 评论(0)

## 25. Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded:

Spring Profiles提供了一种隔离部分应用配置的方式，并让这些配置只在特定的环境生效。任何带有@Profile标记的@Component@Configuration在加载时都会受限制：

@Configuration
@Profile("production")
public class ProductionConfiguration {

// ...

}

In the normal Spring way, you can use a spring.profiles.active Environment property to specify which profiles are active. You can specify the property in any of the usual ways, for example you could include it in your application.properties:

spring.profiles.active=dev,hsqldb

or specify on the command line using the switch --spring.profiles.active=dev,hsqldb.

The spring.profiles.active property follows the same ordering rules as other properties, the highest PropertySource will win. This means that you can specify active profiles in application.properties then replace them using the command line switch.

spring.profiles.active属性与其它的属性一样遵循同样的排序规则，最高的PropertySource优先。这意味着你可以在application.properties指定激活的profiles，然后用命令行转换替代它们。

Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.includeproperty can be used to unconditionally add active profiles. The SpringApplication entry point also has a Java API for setting additional profiles (i.e. on top of those activated by the spring.profiles.active property): see the setAdditionalProfiles() method.

For example, when an application with following properties is run using the switch --spring.profiles.active=prod the proddb and prodmq profiles will also be 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 Section 70.7, “Change configuration depending on the environment” for more details.

### 25.2 Programmatically setting profiles

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

### 25.3 Profile-specific configuration files

Profile-specific variants of both application.properties (or application.yml) and files referenced via @ConfigurationProperties are considered as files are loaded. See Section 24.4, “Profile-specific properties” for details.

application.properties(或application.yml)和通过@ConfigurationProperties引用的文件的特定profiles变种都被当做文件进行加载。更多细节请看24.4小节，“Profile-specific properties”。

个人资料
等级：
访问量： 74万+
积分： 1万+
排名： 1718
个人简介
硕士毕业，目前在外企从事Deep Learning相关工作。
简书
个人博客
微信打赏：
支付宝打赏：
博客专栏
 Spring Framework 5.0.0 参考文档 文章：22篇 阅读：34121 Effective Java_2.0版本 文章：29篇 阅读：20874 机器学习 文章：2篇 阅读：3154 Docker理论与实践 文章：6篇 阅读：5962 Spring Boot 2.0.0参考手册 文章：21篇 阅读：39376 TensowFlow入门与实践 文章：59篇 阅读：138329 Deep Learnig 文章：39篇 阅读：62055 Caffe 文章：24篇 阅读：49976 Linux命令大全 文章：36篇 阅读：41034 Leetcode 文章：18篇 阅读：9580 PyTorch 文章：11篇 阅读：15784 动手学深度学习 文章：11篇 阅读：1859
最新评论