IntelliJ IDEA Spring Boot 多設定檔切換 教學

在 Spring Boot 中的設定檔是 application.properties (或是 application.yaml)

而為了因應多環境有不同的設定,Spring Boot 可以讀取多個設定檔
預設是:application.properties
可以在 application 後面加上「-」符號再加上不同環境的別名來儲存不同的設定檔
例如:application-dev.properties, application-test.properties 等

那在 IntelliJ IDEA 中要怎麼切換不同的設定檔呢?
點選 Select Run/Debug Configuration > Edit Configurations...

選擇到 Spring Boot 的設定檔,點選 Modify options (Alt + M)

點選 Environment variables (Alt + E)

在裡面輸入:
註:記得替換為自己的設定檔名稱,這裡示範的設定檔名稱為:application-dev.properties
  
    spring.profiles.active=dev



儲存後重新執行就會發現讀取的設定檔已經切換了

留言