spring boot 指定profile启动

spring boot项目可为不同的环境配置不同的配置文件,如下图所示:

《spring boot 指定profile启动》

pom.xml配置如下:

<!--配置环境的profile-->
<profiles>
    <!--dev默认激活,使用idea Spring Boot 配置启动工程,需要dev的配置-->
    <profile>
        <id>dev</id>
        <properties>
            <environment>dev</environment>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <environment>test</environment>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <environment>prod</environment>
        </properties>
    </profile>
</profiles>

本地开发使用开发环境,idea启动开发环境配置如下:

1、点击Edit Configrations

《spring boot 指定profile启动》

2、配置如下图

《spring boot 指定profile启动》

3、启动工程

《spring boot 指定profile启动》

控制台打印了application-dev.yml中配置的变量

 

开发时,也有需要一个工程启动多个实例的场景,idea支持一个spring boot项目启动多个实例。

方法非常简单,只需要只需要按照上面的教程在idea再新建一个启动配置,把Active profiles指定为prod即可,如下图:

《spring boot 指定profile启动》

 

通过下图可以看到,本地可以启动多个spring boot 实例

《spring boot 指定profile启动》

 

多环境打包

 

1、运行maven打包命令:

    打包test:mvn clean package -P test
        这样打出来的包中yml文件只会包含:application.yml、application-test.yml
    打包prod:mvn clean package -P prod

        这样打出来的包中yml文件只会包含:application.yml、application-prod.yml

《spring boot 指定profile启动》

2、找到jar包运行

    java -jar 名称.jar –spring.profiles.active=prod

   若打出来的是测试环境的包则运行:java -jar 名称.jar –spring.profiles.active=test

 

    原文作者:Spring Boot
    原文地址: https://blog.csdn.net/u010606397/article/details/80713968
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞