spring boot支持多环境配置文件,在resources下可以定义:application.yml,application-pro.yml,application-test.yml 这样的配置文件,格式为application-xxx.yml,其中后面的xxx就是profile名称,在application.yml中定义spring.profiles.active: test,来表示使用哪个profile的配置文件。
也可以在程序启动时指定,例如执行“java -jar ”命令时添加“-Dspring.profiles.active=test”
spring boot整合maven的profile,在执行maven打包时指定对应的profile,需要把maven pom文件中的变量设置到yml文件。maven的配置如下:
<profiles>
<profile>
<id>test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileName>test</profileName>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<profileName>pro</profileName>
</properties>
</profile>
</profiles>
在application.yml文件中,通过“@”符号引用maven变量:
spring.profiles.active: @profileName@
这样在maven打包时,选择不同的profile