Spring Boot之多环境设置及切换

一、使用properties设置多环境

创建多个properties文件;

application-dev.properties:(开发环境)

server.port=8883

application-test.properties:(测试环境)

server.port=8884

appliction.properties:(主环境)

server.port=8882

 运行主程序:

 可以看出来运行的是主配置文件appliction.properties。那么如果想运行开发环境文件。

在主配置文件中加入:

spring.profiles.active=dev

 则运行配置文件application-dev.properties。

二、使用yml文件设置多环境配置

在yml文件中添加:

server:
  port: 8883
 
---
server:
  port: 8884
spring:
  profiles: dev
  
---
server:
  port: 8885
spring:
  profiles: test

  这样运行主程序运行的还是8883端口。

更改yml文件:

server:
  port: 8883
spring:
  profiles:
    active: dev
---
server:
  port: 8884
spring:
  profiles: dev
  
---
server:
  port: 8885
spring:
  profiles: test

  运行的就是dev环境配置。

注意:如果使用yml文件进行配置,一定要把在properties文件的相关配置注释掉。因为properties文件优先级高于yml文件。

三、动态切换环境:

  i:更改运行参数,在run configurations中。点击arguments ,在 program argument输入

--spring.profiles.active=dev

  run。

  ii:命令行方式

  右击项目:run as->maven build...

  

run。(在这里我出现了问题呜呜呜呜)

  iii:通过vm参数指定环境:

    

原文地址:https://www.cnblogs.com/jccjcc/p/14179782.html