美文网首页
Spring Boot项目多环境配置

Spring Boot项目多环境配置

作者: Haivin | 来源:发表于2019-11-05 22:09 被阅读0次

任何项目都有多环境配置的需求,Spring Boot + maven的项目配置起来也很简单
首先按照在resources文件夹下创建各环境的配置文件,如:

application.yml
application-dev.yml
application-test.yml
application-prod.yml

然后在pom.xml文件里增加如下代码:

<profiles>  
    <profile>  
        <id>dev</id>  
        <activation>  
            <activeByDefault>true</activeByDefault>  
        </activation>  
        <properties>  
            <profileActive>dev</profileActive>  
        </properties>  
    </profile>  
    <profile>  
        <id>test</id>  
        <properties>  
            <profileActive>test</profileActive>  
        </properties>  
    </profile>  
    <profile>  
        <id>prod</id>  
        <properties>  
            <profileActive>prod</profileActive>  
        </properties>  
    </profile>  
</profiles> 

具体环境可根据实际情况进行增减配置,但需要和resources文件夹中的文件名相匹配。
如果使用IDEA进行开发,可以通过选择Maven Projects中的Profiles即可切换环境的配置。如果是测试或生产环境则可以配置maven打包命令指定使用哪个环境的配置文件。如:

mvn clean package -Ptest -U
mvn clean package -Pprod -U

相关文章

网友评论

      本文标题:Spring Boot项目多环境配置

      本文链接:https://www.haomeiwen.com/subject/rktybctx.html