SpringBoot 之Profiles

Spring Profiles提供了一种区别应用配置的部分,使这部分能在特定的环境下生效。任何 @Component , @Configuration or @ConfigurationProperties,能被@Profile标记的注解,限制他们被加载。比如:
@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
Yaml
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.

例如:

Properties
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
Yaml
spring:
  profiles:
  group:
  production:
  - "proddb"
  - "prodmq"

现在我们的应用可以使用--spring.profiles.active=production参数启动,来决定激活production, proddbprodmq 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值