Spring Boot 学习笔记,2.5.Spring Boot 配置——Profile文件多环境支持

一、多Profile文件

  1. 多Profile文件
    多Profile文件配置文件名格式:application-{profile}.properties/yml,默认使用application.properties的配置
  2. 示例.properties格式
    在resources文件夹下新建配置文件application-dev.properties
server.port=8082

在resources文件夹下新建配置文件application-prod.properties

server.port=8083
  1. yml支持多文档块方式,用符号“—”隔开
server:
  port: 8082
---
server:
  port: 8083
spring:
  profiles: dev

---
server:
  port: 8084
spring:
  profiles: prod

二、激活指定Profile

  1. 在配置文件中指定,使用属性:spring.profiles.active
    在application.properties中添加spring.profiles.active=dev,表示使用application-dev.properties这个配置
server.port=8081
spring.profiles.active=dev

在application.yml中添加spring.profiles.active=prod,表示使用application-prod.properties这个配置

server:
  port: 8081
spring:
  profiles:
    active: prod
---
server:
  port: 8082
spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: prod
  1. 命令行:–spring.profiles.active=dev
    在Program arguments中填写–spring.profiles.active=dev
    在这里插入图片描述
    在这里插入图片描述
    如果项目已经打包好,则在启动项目的时候输入:java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
    在这里插入图片描述

  2. 虚拟机参数:-Dspring.profiles.active=prod
    在这里插入图片描述
    在VM options输入:-Dspring.profiles.active=prod
    在这里插入图片描述

发布了23 篇原创文章 · 获赞 5 · 访问量 1452

猜你喜欢

转载自blog.csdn.net/zj499063104/article/details/100655821