所以這必須是一些愚蠢的錯誤,我一直無法通過。我試圖將我的屬性文件外化,當前放置在我的用戶主目錄中。我加載的屬性文件中使用@PropertySource
這樣的:Spring PropertyPlaceHolder Java配置外部屬性文件
@Configuration
@PropertySources(value = { @PropertySource("file:#{systemProperties['user.home']}/.invoice/config.properties") })
public class PropertiesConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
但不幸的是,未加載屬性文件。拋出FileNotFoundException
。但是,如果我將路徑更改爲:
@PropertySources(value = { @PropertySource("file:/home/rohit/.invoice/config.properties") })
它正常工作。這是早期路徑解決的路徑。我已經記錄它進行驗證。所以在我看來,SpEL並沒有在@PropertySource
註釋中得到評估。它應該這樣工作嗎?
如果是,那麼是否有任何其他方式來讀取外部屬性文件,它位於/home/rohit
?我不想給絕對路徑,原因很明顯。我想避免擴展PropertyPlaceHolderConfigurer
類。
我試過的另一個選擇是將/home/rohit/.invoice
文件夾添加到tomcat classpath中。但似乎Spring不使用System Classpath來解析classpath:
後綴。任何關於這個的指針?
該表達式不能使用'file:$ {user.home} /。invoice/config.properties'來代替。 – 2014-11-06 14:55:19
@ M.Deinum讓我試試.. – 2014-11-06 15:01:48
@ M.Deinum Ahaa !!它的工作:)發佈作爲一個答案,所以我可以接受它:) – 2014-11-06 16:16:27