Spring profile通过多种方法实现多环境支持

来自:网络
时间:2020-11-23
阅读:

方式1_使用properties文件:

application.properties:

Spring profile通过多种方法实现多环境支持

application-dev.properties:

Spring profile通过多种方法实现多环境支持

application-pro.properties:

Spring profile通过多种方法实现多环境支持

这种情况是pro配置生效:

Spring profile通过多种方法实现多环境支持

再改变application.properties:

Spring profile通过多种方法实现多环境支持

就是dev生效了

Spring profile通过多种方法实现多环境支持

方式2_使用yml文档块

yml三个横杠就是一个文档块

默认生效第一个文档块:

Spring profile通过多种方法实现多环境支持

Spring profile通过多种方法实现多环境支持

指定dev文档块生效:

Spring profile通过多种方法实现多环境支持

Spring profile通过多种方法实现多环境支持

指定pro文档块生效:

Spring profile通过多种方法实现多环境支持

Spring profile通过多种方法实现多环境支持

方式3_命令行方式(--spring.profiles.active=dev):

注意是杠杠, 两个杠

application.yml中不指定:

server: port: 8084---server: port: 8085spring: profiles: dev---server: port: 8086spring: profiles: pro---

在这里指定

Spring profile通过多种方法实现多环境支持

结果是dev生效:

Spring profile通过多种方法实现多环境支持

或者用cmd命令行:

application.yml:

server: port: 8084---server: port: 8085spring: profiles: dev---server: port: 8086spring: profiles: pro---

Spring profile通过多种方法实现多环境支持

能看到打包的路径:

Spring profile通过多种方法实现多环境支持

使用java -jar命令启动:

Spring profile通过多种方法实现多环境支持

能看到结果是pro配置文件中的8086端口生效

Spring profile通过多种方法实现多环境支持

方式4_虚拟机参数:

固定开头 -D

Spring profile通过多种方法实现多环境支持

Spring profile通过多种方法实现多环境支持

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持免费资源网。

返回顶部
顶部