簡體   English   中英

spring boot 外部配置

[英]spring boot external config

我正在嘗試將外部屬性文件加載到我的 Spring Boot 應用程序中。 最初我在配置類中使用了 @PropertySource。 但現在我想刪除此注釋,以便該類不依賴於位置。 所以我嘗試使用:

java -jar my-boot-ws.war --SPRING_CONFIG_NAME=file:///Users/TMP/resources/

基於這個http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html文檔,但我收到以下錯誤:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder

使用注釋工作正常,但我真的很想擺脫它。 對此的任何幫助都會很棒

謝謝

****** 更正 *******

抱歉,上面的命令應該是復制粘貼錯誤:

java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/

我不想更改配置文件的名稱,只是添加了一個額外的位置。 正如這里所解釋的:

如果 spring.config.location 包含目錄(而不是文件),它們應該以 / 結尾(並且在加載之前會附加從 spring.config.name 生成的名稱)。

我將此解釋為文件 ${spring.application.name}.properties 將從命令行傳入的 --spring.config.location 加載

經過更多的谷歌搜索,我發現這個Spring Boot 和多個外部配置文件表明以下是正確的用法:

java -jar my-boot-ws.war --spring.config.location=file:///Users/TMP/resources/myFile.properties

我的印象是 --spring.config.location 會加載指定目錄中的其他屬性文件。 根據我提到的鏈接中的帖子,情況並非如此。 如果指定了目錄,則基於鏈接,這就是搜索 application.properties 的位置。 但是這里的文檔http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html似乎暗示spring boot 應用程序將在類路徑上查看首先,如果可用,獲取應用程序名稱以獲取基於該名稱的其他屬性文件。

然而,一旦我指定了一個文件名,一切都很好,所以我想我錯了。

在命令行中,您應該使用以下屬性來提及額外的引導配置文件:

--spring.config.location="file:/path/to/application.properties"

另一種選擇是:

-Dspring.config.location="file:/path/to/application.properties"

請注意,字符是小寫的,單詞分隔符是句點 .

否則,您可以使用帶有您已經使用過的密鑰的環境變量:

  • 在 *nix 系統中:

     export SPRING_CONFIG_NAME=file:/path/to/application.properties
  • 在 Windows 操作系統中:

     set SPRING_CONFIG_NAME=file:/path/to/application.properties

這可能不是一個常見問題,但我遇到了。 即使您用--spring.config.name替換它,您的類路徑中也必須有一個application.properties (由於敏感信息,我在 gitignore 中有我的)。

1) 確保 args 在 run 方法內部傳遞

public class GemFireTestLoaderApplication {

public static void main(String[] args) {

        SpringApplication.run(GemFireTestLoaderApplication.class, args);
        }
}

2)如果你已經在xml注釋中配置或者先刪除

<!-- <context:property-placeholder location="classpath:config.properties" />  -->

<!--   <context:property-placeholder location="file:/data/xxx/vaquarkhan/dataLoader/config.properties" /> -->

您可以使用以下命令傳遞屬性名稱

3.1)

java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties

3.2)

java -jar GemfireTest-0.0.1-SNAPSHOT.jar --spring.config.location=file:///C:/data/xxx/vaquarkhan/dataLoader/test/config.properties

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

spring.config.name=spring  
spring.config.location=classpath:/config/

在側面配置文件夾spring.properties文件可用,而在運行服務器時,此屬性文件未加載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM