SpringBoot 多环境配置

多环境配置是一个常见的需求,通常用于区分开发、测试和生产等不同环境的配置。

1. 使用 application-{profile}.properties 或 application-{profile}.yml

在 src/main/resources 目录下创建多个配置文件,文件名格式为 application-{profile}.properties 或 application-{profile}.yml,其中 {profile} 是环境的名称。

例如:

application-dev.properties 或 application-dev.yml:开发环境

application-test.properties 或 application-test.yml:测试环境

application-prod.properties 或 application-prod.yml:生产环境

示例:
application-dev.yml

server:
  port: 8081

application-prod.yml

server:
  port: 8082

2. 激活指定的 Profile

在 application.properties 或 application.yml 中设置 spring.profiles.active 属性来激活指定的 Profile。

示例:
application.yml

spring:
  profiles:
    active: dev

在项目application.yml配置文件中指定激活的Profile为dev,启动项目,控制台输出dev的信息。
SpringBoot 多环境配置_第1张图片

你可能感兴趣的:(spring,boot,spring,boot)