【SpringBoot】14. SpringBoot多环境配置

SpringBoot多环境配置

Spring Boot 1.5.19.RELEASE

假设项目中需要3个环境——开发、测试、生产

profile :代表的就是一个环境变量

语法结构:application-{profile}.properties

需求:

application-dev.properties 开发环境
application-test.properties 测试环境
application-prod.properties 生产环境

具体步骤:

  1. 在src/main/resources/ 新建以上三个文件
#application-dev.properties
server.port=8181
#application-test.properties
server.port=8282
#application-prod.properties
server.port=8383
  1. 将项目打包
    右键项目——Run As——Maven build..

1552914269149.png

打包完成后可在 target 文件夹中看到 springboot-helloworld-0.0.1-SNAPSHOT.jar 文件。

把项目拷贝到某一路径下,例如D盘根目录;

打包时如出现以下错误:

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

https://jingyan.baidu.com/article/6dad5075250734a123e36efa.html

  1. 运行项目
java -jar xxx.jar --spring.profiles.active={profile}

① 打开cmd输入 d: 进入D盘根目录
② 输入上述代码

java -jar springboot-helloworld-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

③ 运行成功,端口为8181端口
④ 重复上述命令dev改成testprod,发现端口号改为了8282和8383

原文地址:https://www.cnblogs.com/isdxh/p/13529212.html