spring_reference/IV. Spring Boot features/24.1. Adding active profile...

16 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

### 24.1. 添加激活的配置(profiles)
spring.profiles.active属性和其他属性一样都遵循相同的排列规则最高的PropertySource获胜。也就是说你可以在application.properties中指定生效的配置然后使用命令行开关替换它们。
有时将特定的配置属性添加到生效的配置中而不是替换它们是有用的。spring.profiles.include属性可以用来无条件的添加生效的配置。SpringApplication的入口点也提供了一个用于设置额外配置的Java API比如在那些通过spring.profiles.active属性生效的配置之上参考setAdditionalProfiles()方法。
示例:当一个应用使用下面的属性,并用`--spring.profiles.active=prod`开关运行那proddb和prodmq配置也会生效
```java
---
my.property: fromyamlfile
---
spring.profiles: prod
spring.profiles.include: proddb,prodmq
```
**注**spring.profiles属性可以定义到一个YAML文档中用于决定什么时候该文档被包含进配置中。具体参考[Section 63.6, “Change configuration depending on the environment”](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-change-configuration-depending-on-the-environment)