方式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通过多种方法实现多环境支持

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。