springboot 多环境切换
properties文件
profile是Spring对不同环境提供不同装备功用的支持,可以经过激活不同的环境版本,实现快速切换环境;
例如:
application-test.properties 代表测验环境装备
application-dev.properties 代表开发环境装备
但是Springboot并不会直接启动这些装备文件,它默许使用application.properties主装备文件;
我们需求经过一个装备来选择需求激活的环境:
spring.profiles.active=dev
spring.profiles.active可以指定装备环境,如指定为dev就会选择 application-dev.properties 文件作为装备环境,同理,要指定不同的环境装备,只需指定application-xxx.properties的xxx即可。
如此时创建不同的装备文件
在主配置文件中参加spring.profiles.active=dev,
在 application-dev.properties 中参加:
server.port=8081
在 application-test.properties 中加入
server.port=8082
启动项目,可以看到端口设置为了8081

springboot 启动会扫描以下方位的application.properties或许application.yml文件作为Spring boot的默许配置文件:
yaml文件
yaml文件切换配置环境与properties文件有所不同,它可以在同一个文件中配置多套环境,只要给每套环境设置称号,在主配置环境中指定即可:
erver:
port: 8081
#选择要激活那个环境
spring: profiles:
active: test
---
server:
port: 8083
spring:
profiles: dev
#配置环境的名称
---
server:
port: 8084
spring:
profiles: test #配置环境的名称