spring boot spring.profiles 是针对应用程序,不同环境需要不同配置加载的一种解决方案。

以下是我的配置文件

properties命名规范一定是application-开头,然后是你的环境配置命名dev,例如:
开发环境 :application-dev.properties
测试环境 :application-test.properties

然后在主application.properties配置你指定的生效的文件配置。


在主配置文件下面添加spring.profiles.active=你的配置文件,不需要全名。
例如:application-dev.properties
spring.profiles.active=dev 即可

运行项目发现The following profiles are active: dev,配置文件已激活生效,可是port和context path没有变化,还是原来的默认配置,也就是说我们的application-dev.properties这个没有生效。

在spring boot官网上找到这么一段话。

就是说你需要用profiles来指定你的配置文件

   		  <profiles>
            <profile>dev</profile>
            <profile>test</profile>
          </profiles>

也可以使用在终端输入 mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar 来设置你的配置文件。

再次运行配置生效。

在浏览器输入 localhost:8081/dev/test查看结果

结束语: 可加微信进学习互帮互助群 Benjamin19950304

更多推荐

spring boot spring.profiles.active不生效解决方案