@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {
// ...
}
如果@ConfigurationProperties beans是通过@EnableConfigurationProperties注解被加载,自动扫描,@Profile注解需定义在有这个@EnableConfigurationProperties注解的@Configuration class类上。@Profile能被定义@ConfigurationProperties class类自己上,@ConfigurationProperties被扫描。
你能使用spring.profiles.active Environment属性定义那个profiles被激活。你能具体描述这个属性在任何方式更早的描述,在这一章节。例如:
Properties
spring.profiles.active=dev,hsqldb
spring:
profiles:
active: "dev,hsqldb"
你也可以在命令行添加--spring.profiles.active=dev,hsqldb,来决定哪个profiles被激活。
Adding Active Profiles
spring.profiles.active属性遵循与其他属性同样的顺序规则:最高的资源胜(个人理解,优先加载它)。这意味着你能具体描述在application.properties,然后替换他们通过命令行。
有些时候,添加active profiles这个属性比替换他们是非常有用的。SpringApplication入口Java Api
提供了设置这个setAdditionalProfiles()方法。
Profile Groups
有些时候,你定义的和你使用的profiles在你应用中是细粒度和复杂的。例如你可能有proddb和prodmq在独立的场景中使用。
为了实现这个,SpringBoot 让你定义profiles groups 一个profile group 允许你定义一个逻辑名字对一个相关的group.
例如:
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
spring:
profiles:
group:
production:
- "proddb"
- "prodmq"
现在我们的应用可以使用--spring.profiles.active=production参数启动,来决定激活production, proddb 和prodmq profiles。
Programmatically Setting Profiles
你能程序化的设置active profiles 通过SpringApplication.setAdditionalProfiles(… )在你的应用运行之前。也可以通过Spring’s ConfigurableEnvironment接口实现设置。
Profile-specific Configuration Files
profile-specific 的两种变种,application.properties (or application.yml) 还有通过@ConfigurationProperties注解被考虑加载的文件。具体详情见Profile Specific Files