spring.profiles.active=@profiles.active@的含義


spring.profiles.active=@profiles.active@ ,其實是配合 maven profile進行選擇不同配置文件進行啟動。

當執行

mvn clean package -P test 命令時, @profiles.active@ 會替換成 test

打開 jar包,即可看到:

 

 

 

實戰

1.構建一個springboot 項目

這里使用idea進行構建的,這個過程省略

2.pom文件配置

<profiles>
        <profile>
            <!-- 生產環境 -->
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 本地開發環境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <!-- 測試環境 -->
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
    </profiles>
  • 這里默認dev配置

3.配置多個配置文件

application.properties

注意這里的profiles.active 要和pom文件的對應上

spring.profiles.active=@profiles.active@

application-dev.properties

name = "dev"

application-prod.properties

name = "prod"

application-test.properties

name = "test"

4.編寫個測試的controller


/**
 * @author kevin
 * @date 2019/6/28 16:12
 */
@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @RequestMapping(value = {"/hello"},method = RequestMethod.GET)
    public String say(){
        return name;
    }
}

5.啟動測試

使用idea工具啟動開發

默認是dev,假如想要使用prod配置文件,如上圖選擇prod,注意下面的導入,重啟項目

D:\dev_code\profiles-demo\target>curl http://localhost:8080/hello
"prod"

6 打包

這里使用idea打包不再介紹,如果你使用命令

mvn clean package -P dev

則是使用dev配置

 

 

 

參考文章:http://www.likecs.com/show-62694.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM