一、多环境开发(单文件yml或yaml版)
1、简介
- 创建一个配置文件,使用 - - - 区分环境设置边界,在该配置文件中引用
2、代码案例
# 应用环境
spring:
profiles:
active: test
---
# 生产环境
spring:
profiles: pro
server:
port: 80
---
# 开发环境
spring:
profiles: dev
server:
port: 81
---
# 测试环境
spring:
profiles: test
server:
port: 82
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
二、多环境开发(多文件yml或yaml版)
1、简介
- 需要创建多个配置文件,在主配置文件中进行引用
- 文件命名要 application-xxx.yml
2、代码案例
- 主启动配置文件 application.yml
spring:
profiles:
active: test
- 生产环境配置文件 application-pro.yml
server:
port: 80
- 开发环境配置文件 application-dev.yml
server:
port: 81
- 测试环境配置文件 application-test.yml
server:
port: 82
三、多环境开发(多文件properties版)
1、简介
- 需要创建多个配置文件,在主配置文件中进行引用
- properties 仅支持多文件版
- 文件命名要 application-xxx.properties
2、代码案例
- 主启动配置文件 application.properties
spring.profiles.active=pro
- 生产环境配置文件 application-pro.properties
server.port=80
- 开发环境配置文件 application-dev.properties
server.port=81
- 测试环境配置文件 application-test.properties
server.port=82
四、多环境分组管理
1、简介
- SpringBoot2.4以后开始使用group属性代替了原来的include属性,降低了配置书写量
- 多环境开发使用group属性设置配置文件分组,便于线上维护管理
- 使用group属性定义多种主环境与子环境的包含关系
- 文件命名要 application-xxx.yml
- 后面覆盖前面
2、代码案例
- 主启动 application.yml
spring:
profiles:
# 选择要执行的组,后面覆盖前面
active: dev,pro
# 分组
group:
# 定义分组,后面覆盖前面
"dev": devDB,devMVC
"pro": proDB,proMVC
"test": testMVC,testDB
- application-devDB.yml
server:
port: 80
- application-devMVC.yml
server:
servlet:
# 上下文路径http://localhost:81/my/*
context-path: /my
port: 81
- application-proDB.yml
server:
port: 82
- application-proMVC.yml
server:
servlet:
# 上下文路径http://localhost:83/you/*
context-path: /you
port: 83
- application-testDB.yml
server:
port: 84
- application-testMVC.yml
server:
servlet:
# # 上下文路径http://localhost:85/it/*
context-path: /it
port: 85
五、多环境开发Maven控制
1、配置Maven
<!--设置多环境-->
<profiles>
<profile>
<id>env_dev</id>
<properties>
<profile.acctive>dev</profile.acctive>
</properties>
</profile>
<profile>
<id>env_pro</id>
<properties>
<profile.acctive>pro</profile.acctive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2、引用Maven属性 @xxx@
3、执行Maven指令compile
- 基于SpringBoot读取Maven配置属性的前提下,如果在IDEA下测试工程时pom.xml每次更新需要手动compile方可生效,IDEA中的bug