首页 > 编程语言 > 详细

Spring Boot之多环境设置及切换

时间:2020-12-23 19:19:25      阅读:37      评论:0      收藏:0      [点我收藏+]

一、使用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参数指定环境:

    技术分享图片

 

Spring Boot之多环境设置及切换

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!