2017-07-08 109 views
1

我有配置文件位於除我的jar文件以外的其他目錄中的問題。@PropertySource無法打開,因爲它不存在

我使用@PropertySource來加載屬性。

@PropertySource(ignoreResourceNotFound = true, value = "${ext.properties.dir:classpath:}/properties.properties") 

我嘗試使用下面的命令來運行的jar:

java -jar import-0.0.1-SNAPSHOT.jar -Dext.properties.dir=file:/C:\Users\Admin\Desktop\ 

以下錯誤普林在日誌:屬性位置

屬性位置 [$ {ext.properties.dir: classpath:}/properties.properties]無法解析: 無法打開類路徑資源[properties.properties],因爲它不存在 不存在

我該如何解決這個錯誤?

+0

這可能會幫助你 - https://stackoverflow.com/questions/33184807/override-properties-file-in-spring-webapp-at-runtime – Arpit

+0

@Arpit,我做了與答案中相同的配置,但我得到了'不能打開,因爲它不存在' –

+0

你使用的是'spring mvc'還是'spring boot'? – elmigue017

回答

1

我看到你正在使用應用,根據spring documentation你可以嘗試使用這種環境屬性:

--spring.config.location=file:/path/location/file-name.properties 

最後的指令是:

java -jar import-0.0.1-SNAPSHOT.jar --spring.config.location=file:C:\Users\Admin\Desktop\import.properties 
+0

'spring.config.location'不會幫助他使用'@ PropertySource' –

+0

您是否得到相同的錯誤或者是否有所不同?你檢查@ AbhijitSarkar的建議嗎? –

+0

@JUANCALVOPINAM,謝謝,'--spring.config.location'很好用 –

1

春季啓動查找在類路徑上。您不需要明確的@PropertySource註釋。並且@PropertySource不具備解析路徑中的SPEL的功能,這應該從您收到的錯誤消息中看出。使用src/main/resources/application.properties。當你想使用外部文件時,請使用spring.config.location,比如他的答案中建議的@ juan-calvopina-m。

相關問題