1、在父项目的pom.xml中配置

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <multi-env>dev</multi-env>
            </properties>
        </profile>
        <profile>
            <id>yufa</id>
            <properties>
                <multi-env>yufa</multi-env>
            </properties>
        </profile>
        <profile>
            <id>online</id>
            <properties>
                <multi-env>online</multi-env>
            </properties>
        </profile>
    </profiles>

2、在子项目的resources中配置三个yml文件,分别是

内容分别为:

application-online.yml  中:
test:
 variable:
  This is online mode


application-dev.yml  中:
test:
 variable:
  This is development mode


application-yufa.yml  中:
test:
 variable:
  This is yufa mode

3、在application.yml  写入:

spring:
 profiles:
  active: @multi-env@

4:取值

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @Value("${test.variable}")
    private String multiEnv;

    @RequestMapping("test")
    @ResponseBody
    public String test(){
        return multiEnv;
    }
}

5、注意在运行前先选择

6、结果:

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐