SpringBoot激活配置文件切换

本文介绍了SpringBoot中如何管理不同环境的配置,包括在pom.xml中定义profile,以及在yaml和properties文件中使用这些profile。在2.4版本后,引入了Profile组概念,允许一起激活多个配置文件。详细阐述了配置文件的分割、激活以及include的使用,以及在2.3版本下如何兼容旧的配置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.yml配置方式

如果使用pom文件指定环境则:

 <profiles>
        <profile>

            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <!--当前环境为默认环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
    </profiles>

然后在yaml文件中使用占位符进行替换

##设置默认的激活配置文件
spring:
 profiles:
  ##这里会把xml文件中的默认配置名注入进来,也可以在这写死注入
  default: @profiles.active@

server:
  port: 9000
---
#配置模拟环境下的端口
spring:
 config:
  activate:
   on-profile: "dev"
server:
 port: 9001
---
spring:
 config:
  activate:
   on-profile: "prod"
server:
 port: 9002
---
spring:
 config:
  activate:
   on-profile: "test"
server:
 port: 9003

---

spring:
 datasource:
  driver-class-name: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://localhost:3306/emp?allowMultiQueries=true&useAffectedRows=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
  username: ****
  password: ***

在Spring2.4过后,yaml支持使用---用来分割多个不同的配置文档,而properties则是使用#---进行分割.当我们在读取的时候就会对应profile进行读取.

2.properties配置方式

pom配置同上,不过就是配置文件写法不一样.

在properties中,分割文档的话会报错,所以不能使用yaml的那种方法,所以还是需要创建-dev或者-pro等等的配置文件,然后使用active声明使用哪个配置文件.

server.port=9000
spring.profiles.active=dev

同时在-dev的配置文件中声明

spring.config.activate.on-profile=dev
server.port=9001

这样就会将前面重复的配置覆盖.

3.include

spring.profiles.include:该api是用来启动多个配置文件的,打个比方,我有dev1文件和dev2文件,分别配置了端口和mybatis url,我如果要同时启动这两个的话就需要用到该方法.

由于Spring Boot 2.4不能同时设置spring.config.activate.on-profile和spring.profiles.active,也不能同时设置spring.config.activate.on-profile和spring.profiles.include,那对于2.3版本使用spring.profiles + spring.profiles.include扩展激活profiles这种情况怎么兼容呢?

spring:
  config:
    activate:
      on-profile: "debug"
  ##启动多个配置文件组
  profiles:
    include: "debugdb,debugcloud"

##上面写法是错误的SpringBoot不支持两个在一起
==============================================
spring:
  profiles:
    group:
      "debug": "debugdb,debugcloud"

---
spring:
  config:
    activate:
      on-profile: "debug"
使用Profile组

为了解决这类问题,Spring Boot 2.4 提出Profile组概念,Profile组可以完成这样一件事:如果profile 'x’处于激活状态,profiles ‘y’ 和 ‘z’ 也处于激活状态。配置项格式为spring.profiles.group.<source>,同样,也不能同时设置配置项spring.profile.groupspring.config.activate.on-profile的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值