天天看點

idea springboot多環境配置

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檔案,分别是

idea springboot多環境配置

内容分别為:

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、注意在運作前先選擇

idea springboot多環境配置

6、結果:

idea springboot多環境配置

繼續閱讀