使用profile标签动态引入环境变量

本文介绍了如何在Spring Boot中利用Maven的profile标签动态引入环境变量。详细讲述了项目结构、pom.xml的依赖配置、application.properties文件的设置,以及ProfiletestApplication和TestController的代码实现。通过选择不同的profile id,实现了不同环境的配置切换。在实际项目打包时,可以通过mvn命令指定激活的profile。

使用profile标签动态引入环境变量

1.项目结构

在这里插入图片描述

2.具体代码

对应关系简图
在这里插入图片描述

2.1pom依赖

<?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 https://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.5.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>profiletest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>profiletest</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>

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

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

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

                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <profiles>
        <profile>
            <id>1</id>
            <properties>
                <port>8181</port>
                <spring.profile>1</spring.profile>
            </properties>
        </profile>


        <profile>
            <id>2</id>
            <properties>
                <port>9090</port>
                <spring.profile>2</spring.profile>
            </properties>
        </profile>
    </profiles>

</project>

2.2 application.properties

server.port=@port@
spring.profile=@spring.profile@

2.3 ProfiletestApplication

package com.example.profiletest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProfiletestApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProfiletestApplication.class, args);
    }

}

2.4 TestController

package com.example.profiletest.controller;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController()
@RequestMapping("/test")
public class TestController {


    @Value("${spring.profile}")
    private String profileString;


    @GetMapping("/1")
    private String get1(){
        return profileString;
    }
}

3.实际验证结果

运行时选择
在这里插入图片描述

3.1 选择id为1时

在这里插入图片描述

3.2 选择id为2时

在这里插入图片描述

4.profile标签的其余作用(待补充)

还可以指定要加载的dependencies,在idea右边mvn栏选择运行要激活的profile标签的id值即可

5.项目具体打包时mvn命令指定profile

mvn clean package -D maven.test.skip=true -P product其中-P指定的就是要激活的profile标签的id值

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值