一、YML配置文件
1、单文件配置多环境以“---”分割
spring:
profiles:
active: pro
---
# 设置生产环境
spring:
profiles: pro
server:
port: 80
---
# 设置开发环境
spring:
profiles: dev
server:
port: 81
---
# 设置测试环境
spring:
profiles: test
server:
port: 82
切换环境再测试
2、多文件配置多环境
开发环境配置文件:application-dev.yml
# 设置开发环境
server:
port: 81
生产环境配置文件:application-pro.yml
# 设置生产环境
server:
port: 80
测试环境配置文件:application-test.yml
# 设置测试环境
server:
port: 82
应用环境配置文件:application.yml
# 应用的环境
spring:
profiles:
active: test
测试结果
切换环境再测试
二、properties配置文件
1、多文件配置配置多环境
开发环境配置文件:application-dev.properties
# 开发环境
server.port=8001
生产环境配置文件:application-pro.properties
# 生产环境
server.port=8000
测试环境配置文件:application-test.properties
# 测试环境
server.port=8002
应用环境配置文件:application.properties
# 应用的环境
spring.profiles.active=pro
测试结果
切换环境再次测试
三、多环境分组管理
1、配置文件组合
application-dev.yml
server:
port: 80
application-devDB.yml
# 数据库相关的开发环境
server:
port: 81
application-MVC.yml
server:
servlet:
context-path: /back
port: 82
application.yml
spring:
profiles:
active: dev
include: devMVC,devDB
测试
2、配置文件分组