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: @[email protected]
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、结果: