Springboot基础模块:多环境配置

1 pom.xml

<project>
<profiles>
    <profile>
      <id>dev</id>
      <properties>
        <spring.profiles.active>dev</spring.profiles.active>
      </properties>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>
    <profile>
      <id>test</id>
      <properties>
        <spring.profiles.active>test</spring.profiles.active>
      </properties>
    </profile>
    <profile>
      <id>pre</id>
      <properties>
        <spring.profiles.active>pre</spring.profiles.active>
      </properties>
    </profile>
    <profile>
      <id>rc</id>
      <properties>
        <spring.profiles.active>rc</spring.profiles.active>
      </properties>
    </profile>
  </profiles>
</project>

2 配置文件

2.1 自动指定环境

2.1.0 使用双引号

打包时,添加-P参数,指定环境的配置文件,用于Jekins自动化部署。

  • application.yml
spring:
  application: 
    name: user-system 
  profiles:
    active: "@spring.profiles.active@"

2.1.2 不使用双引号

  • 异常
    idea Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ‘@’ that cannot start any token
  • 方案:添加依赖
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>1.26</version>
</dependency>
  • application.yml
spring:
  application: 
    name: user-system 
  profiles:
    active: @spring.profiles.active@

2.2 环境配置文件

# 开发环境
application-dev.yml
# 测试环境
application-test.yml
# 预发布环境
application-pre.yml
# 发行环境
application-rc.yml

2.3 运行

2.3.1 命令运行

# 指定环境
mvn spring-boot:run -Dspring-boot.run.profiles=dev
  • 打包
mvn clean package -P dev
  • 运行
# 指定环境
java -jar -Dspring.profiles.active=dev ***.jar

2.3.2 自动运行

在这里插入图片描述

图1 IDEA多环境配置

2.2 手动指定环境

可以配置多环境文件,打包时,指定特定的环境,不适合自动部署,适用于开发调试。

  • application.yml
spring:
  application: 
    name: user-system 
  profiles:
    active: dev
  • 环境配置文件:
# 开发环境
application-dev.yml
# 测试环境
application-test.yml
# 预发布环境
application-pre.yml
# 发行环境
application-rc.yml
  • 运行
java -jar **.jar

【参考文献】
[1]https://www.cnblogs.com/zhwanwan/p/11898524.html
[2]https://www.cnblogs.com/lizm166/p/10749023.html
[3]https://www.jianshu.com/p/bb8ac8abddc3

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值