springboot中Profile多环境切换配置

springboot中Profile多环境切换配置

1、 假如项目中有application.properties,application-dev.properties,application-test.properties三个文件。
springboot默认读取的配置文件为application.properties,如果要使用application-dev.properties中的配置,则可在application.properties中添加spring.properties.active=dev即可。

application.properties文件配置:配置了端口8080,以及指定的环境是dev。
springboot中Profile多环境切换配置

application-dev.properties文件配置 :配置了端口8082
springboot中Profile多环境切换配置

application-test.properties文件配置 :配置了端口8083
springboot中Profile多环境切换配置

最终启动项目端口号选择了8082:

2、 yml文件。yml中的多环境都配置在application.yml文件中,环境之间以“- - -”(三个杠)分开。使用spring.profiles来标识一个环境。spring.profiles.active=dev,指定选用dev环境(可在application.properties和application.yml中指定)如下图:
springboot中Profile多环境切换配置