springboot 多环境配置

1.案例简单介绍

2.不借助maven的profile

  • 2.1 application.yml中的内容----->这里指定dev为默认环境配置
spring:
  profiles:
    active: dev
  • 2.2 打包----->在项目根目录下运行如下命令
mvn clean package -Dmaven.test.skip=true
  • 2.3 项目部署 ----->在jar包所在目录运行如下命令
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod

等号后面等于prod则实际使用的是application-prod.yml文件里的配置,等于test则实际使用的是application-test.yml文件里的配置,以此类推,去掉 --spring.profiles.active=prod,则使用默认的application-dev.yml文件里的配置.

3.借助maven的profile标签

  • 3.1 pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nrsc.application</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <profiles>
        <!-- ++++++++++++++++++++++++++多环境配置++++++++++++++++++++++++++++++++++ -->
        <profile>
            <id>dev</id>
            <properties>
                <packaging>jar</packaging>
                <activatedProperties>dev</activatedProperties>
                <main-class>com.nrsc.application.demo.DemoApplication</main-class>
            </properties>
            <activation>
                <!--默认使用的环境-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <packaging>jar</packaging>
                <activatedProperties>test</activatedProperties>
                <main-class>com.nrsc.application.demo.DemoApplication</main-class>
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <packaging>jar</packaging>
                <activatedProperties>uat</activatedProperties>
                <main-class>com.nrsc.application.demo.DemoApplication</main-class>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <packaging>jar</packaging>
                <activatedProperties>prod</activatedProperties>
                <main-class>com.nrsc.application.demo.DemoApplication</main-class>
            </properties>
        </profile>
        <!-- ++++++++++++++++++++++++++多环境配置++++++++++++++++++++++++++++++++++ -->
    </profiles>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <!--指定打包的名称-->
        <!--<finalName>application-demo</finalName> -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 3.2 application.yml中需要修改成下面的样子
spring:
  profiles:
    active: @activatedProperties@
  • 3.3 打包和部署同不借助maven的profile标签中内容相同.

4. 两者区别

我所知道的区别主要在idea的显示上,描述如下:
在这里插入图片描述

5 本demo的地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值