SpringBoot 之多环境切换

java -jar xxx.jar --server.port=8080
java -jar xxx.jar --server.port=8081

上面的启动命令可以指定配置属性。

1、SpringBoot自带指定环境

多profile文件形式

  • 格式:application-{profile}.properties/yml,如:application-dev.properties、application-prd.properties等。
  • Spring Boot 默认都是从全局配置文件 application.propertiesapplication.yml 进入开始读取配置。
  • 可以使用约定格式application-{profile}.properties/yml写任意多的配置文件。
  • 然后在全局配置文件 application.properties/yml 激活它们即可,使用spring.profiles.active=dev

application-dev.properties

test.name=vermouth.dev
server.port=8081

application-prd.properties

test.name=vermouth.prd
server.port=8082

yml文档块形式

yml 文件支持多文档块方式,同一个yml文件中,可以使用"—"来区分不同的文档,相当于不同的配置文件。
application.yml

spring:
  profiles:
    active: prd

---
server:
  port: 8083
spring:
  profiles: dev

---
server:
  port: 8084
spring:
  profiles: prd  #指定属于哪个环境

激活指定profile的几种方式

  • 在全局配置文件中指定:spring.profiles.active=dev
  • 命令行:java -jar spring-boot.jar --spring.profiles.active=dev
  • 在IDEA中配置虚拟机参数:-Dspring.profiles.active=dev

上面的配置会激活application-dev.properties配置文件。

2、通过POM自定义属性值指定环境

把环境信息配置在不同的文件夹中,在编译打包时根据pom配置加载不同文件夹下的配置文件即可。
1、创建不同环境的配置文件夹及配置文件
在这里插入图片描述
2、修改pom文件

<properties> 
    <env>prd</env>
</properties>
    
<build>
	<!-- 指定打包后的文件名-->
	<finalName>${project.artifactId}-${env}-${project.version}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/conf/${env}</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

3、编译打包
指定evn值为prd,发现只加载了conf/prd文件夹下的配置文件。
同理,如果指定env值为dev,只会加载conf/dev文件夹下的配置文件。
在这里插入图片描述

3、打包时指定POM中的profile值指定环境

把环境信息配置在不同的配置文件中,在编译打包时加载不同的配置文件即可。
1、创建多个配置文件
application.properties:

spring.profiles.active=@profileActive@

application-dev.properties:

test.name=vermouth.dev

application-prd.properties:

test.name=sherry.prd

2、修改pom文件

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>application*.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <!-- 根据打包时激活的profile来包括对应的配置文件 -->
                <include>application.properties</include>
                <include>application-${profileActive}.properties</include>
            </includes>
        </resource>
    </resources>
</build>

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <!-- 如果未指定环境,默认dev-->
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <profileActive>dev</profileActive>
        </properties>
    </profile>
    <profile>
        <id>prd</id>
        <properties>
            <profileActive>prd</profileActive>
        </properties>
    </profile>
</profiles>

3、打包
命令打包指定环境:

mvn clean package -P prd

IDEA 工具打包指定环境:

如下图可见,打包后就只加载了application.properties和application-prd.properties,且application.properties文件中的spring.profiles.active=prd
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值