Spring Boot学习06多环境配置

方式一:使用多个properties文件

定义如下三个用于不同环境的配置文件

1、application-dev.properties    用于开发环境

server.port=8081

2、application-test.properties    用于测试环境

server.port=8082

3、application-pro.properties    用于生产环境

server.port=8083

在主配置文件application.properties中,激活对应的配置文件:

#激活application-dev.properties 配置文件
spring.profiles.active=dev

#激活application-test.properties 配置文件
#spring.profiles.active=test

#激活application-pro.properties 配置文件
#spring.profiles.active=pro

启动端口为:8081

方式二:在application.yml文件中定义多个节

server:
  port: 8080
spring:
  profiles:
    active: test
---
server:
  port: 8081
spring:
  config:
    activate:
      on-profile: dev
---
server:
  port: 8082
spring:
  config:
    activate:
      on-profile: test
---
server:
  port: 8083
spring:
  config:
    activate:
      on-profile: pro

启动端口为:8082

方式三:使用虚拟机参数配置

这样设置,启动端口是8083

方式四:使用命令行参数

对应的命令为:java -jar xxx.jar --spring.profiles.active=dev

这样设置,启动端口是8081(此步设置需要先删除第三步中的虚拟机参数的设置)

原文地址:https://www.cnblogs.com/asenyang/p/15450209.html