profile简介
在 Spring Boot 中,Profile(配置文件) 是用于实现多环境配置隔离的核心机制,它允许开发者为不同的运行环境(如开发、测试、生产)定义差异化的配置,从而实现一套代码在多环境下的灵活部署。
Profile 的核心作用
- 环境隔离:将开发、测试、生产等环境的配置(如数据库连接、日志级别、服务端口等)分开管理,避免配置冲突。
- 灵活切换:启动应用时可指定激活的 Profile,自动加载对应环境的配置。
2. 配置文件命名规则
Spring Boot 通过配置文件的命名后缀来区分不同 Profile 的配置
- 开发环境:
application-dev.yml - 测试环境:
application-test.yml - 生产环境:
application-prod.yml - 通用配置(所有环境共享):
application.yml
在3.6.5中,更新了书写方式,不再使用profiles中的activate,而是更改到了config中。
# 全局默认配置块(单独分隔,不指定任何环境)
spring:
profiles:
active: test # 这里才是真正的默认激活配置
--- # 用---分隔,明确区分默认配置和dev环境
# dev环境配置
server:
port: 8081
spring:
config:
activate:
on-profile: dev
--- # 分隔test环境
# test环境配置
server:
port: 8082
spring:
config:
activate:
on-profile: test
--- # 分隔prod环境
# prod环境配置
server:
port: 8083
spring:
config:
activate:
on-profile: prod
799

被折叠的 条评论
为什么被折叠?



