springboot profiles


springboot profiles

                                 

官网:Spring Boot Features(Profiles)

                               

                                

*****************

profiles 简介

                   

Spring Profiles provide a way to segregate parts of your application 
configuration and make it be available only in certain environments. 
# profile只在特定的环境下激活,实现对应用配置的分离

Any @Component, @Configuration or @ConfigurationProperties can be 
marked with @Profile to limit when it is loaded
# 可以在创建的时候用@Profile标记@Component, @Configuration or @ConfigurationProperties

If @ConfigurationProperties beans are registered via @EnableConfigurationProperties 
instead of automatic scanning, the @Profile annotation needs to be specified on the 
@Configuration class that has the @EnableConfigurationProperties annotation
# 如果@ConfigurationProperties是用@EnableConfigurationProperties创建的,则需要在
# @EnableConfigurationProperties所在的类上加上注解@Profile

                         

示例

​
@Configuration(proxyBeanMethods = false)
@Profile("production")        //以下代码只在production激活时使用
public class ProductionConfiguration {

    // ...

}

                      

                               

*****************

profile 激活

                           

配置文件激活profile

spring:
  profiles:
    active: "dev,hsqldb"  # 同时激活dev,hsqldb

                             

命令行激活profile

java -jar demo.jar ----spring.profiles.active=dev,hsqldb

                 

设置默认激活属性

spring:
  profiles:
    default: "dev"  #如果没有设置spring.profiles.active,则默认激活dev

                                   

                                                       

*****************

profile group

spring:
  profiles:
    group:
      production:
      - "proddb"
      - "prodmq"

# production组包含proddb、prodmq
profile group:production
profile:proddb、promq

激活production时(--spring.profiles.active=production),可同时激活proddb、prodmq

                             

                                           

おすすめ

転載: blog.csdn.net/weixin_43931625/article/details/120266291