温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot ------------spring.profiles.active 分区配置

发布时间:2020-04-30 20:51:41 来源:网络 阅读:1478 作者:一排小牙印 栏目:软件技术

Spring Boot 的 Profiles 用于分区配置

好处:可以通过spring.profiles.active 进行不同环境切换

配置位置:Spring Boot 项目下 application.properties

配置格式: (application-{profile}.properties) 会默认按照配置加载相应的配置文件

配置示例:
application-dev.properties
application-test.properties
application-prod.properties

              spring.profiles.active=test 此时读取application-test-properties文件
              spring.profiles.active: prod,proddb,prodmq  同时激活三个配置

扩展:spring.profiles.include 用于叠加profile


配置加载方式

  1. 在application.properties中固定写死
  2. 通过调用执行命令切换环境调用

通常情况下第二种方式更适用于正式研发,代码如下:

1、更改application.xml
由spring.profiles.active=dev更改为spring.profiles.active=@profileActive@

2、更改pom.xml
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
<maven.test.skip>true</maven.test.skip>
<scope.jar>compile</scope.jar>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
<maven.test.skip>true</maven.test.skip>
<scope.jar>provided</scope.jar>
</properties>
</profile>
<profile>
<id>demo</id>
<properties>
<profileActive>demo</profileActive>
<maven.test.skip>true</maven.test.skip>
<scope.jar>provided</scope.jar>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<profileActive>pro</profileActive>
<maven.test.skip>true</maven.test.skip>
<scope.jar>provided</scope.jar>
</properties>
</profile>
3、在maven打包的时候执行命令
mvn clean ×××tall -Dmaven.test.skip=true -Ptest

4、在application 执行命令
-Dspring.profiles.active=dev

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI