25.1 Adding Active Profiles
spring.profiles.active
属性遵循其他属性相同的排序规则:最高的PropertySource
获胜.这意味着您可以在application.properties中指定激活的配置文件,然后使用命令行参数替换它们.
有时,将特定配置文件的属性添加到激活配置文件中而不是替换它们将会非常有用.spring.profiles.include
属性可用于无条件添加至活动配置文件.
SpringApplication
类入口点还有一个Java API用于设置额外的配置文件(也就是说,该配置的文件都在spring.profiles.active
配置的概要文件之上).
在SpringApplication中查看setAdditionalProfiles()
方法.
例如,当使用命令行参数--spring.profiles.active=prod
运行应用程序时,proddb和prodmq配置文件也被激活:
my.property: fromyamlfile
---
spring.profiles: prod
spring.profiles.include:
- proddb
- prodmq
Note
请记住,
spring.profiles
件属性可以被定义在YAML文件来确定这个特定的文档包含在配置.见Section 74.7 Change Configuration Depending on the Environment获取更多的细节.