我有一個Spring Boot應用程序,它可以在各種環境中運行,並且基於它運行的環境,它將連接到不同的數據庫。我有幾個application.properties
文件,一個爲每個環境看起來像這樣:基於環境變量設置Spring Boot application.properties
application-local.properties
:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=dbuser
spring.datasource.password=123456789
application-someserver.properties
:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass
等等,等等
在我的每一個環境中,我有一個名爲MYENV
的環境變量,它被設置爲它所處的環境類型,大量的local
或someserver
(application-{env}.properties
文件的名稱完全匹配環境名稱)。
我該如何獲得spring boot來讀取這個環境變量並自動選擇正確的.properties
文件?由於這個軟件包被部署的方式(它不會像jar一樣運行),所以我不想做整個-Dspring.profiles.active=someserver
。
是否有可能使在環境變量不匹配任何文件,選擇標準有一個「默認」的配置? –
'spring.profiles.active = $ {MYENV:defaultValue}' –
另外,將默認配置和通用配置放入'application.properties'中。它總是會被使用,但在配置文件特定配置的優先級較低 –