2014-11-06 57 views
5

所以這必須是一些愚蠢的錯誤,我一直無法通過。我試圖將我的屬性文件外化,當前放置在我的用戶主目錄中。我加載的屬性文件中使用@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:後綴。任何關於這個的指針?

+1

該表達式不能使用'file:$ {user.home} /。invoice/config.properties'來代替。 – 2014-11-06 14:55:19

+0

@ M.Deinum讓我試試.. – 2014-11-06 15:01:48

+0

@ M.Deinum Ahaa !!它的工作:)發佈作爲一個答案,所以我可以接受它:) – 2014-11-06 16:16:27

回答

3

@PropertySoure註釋EL表達式不起作用。您可以使用佔位符${...},但也限制爲系統或環境變量。但是,如果您想解析用戶的主目錄,則可以使用${user.home}佔位符。

@PropertySource("file:${user.home}/.invoice/config.properties") 

這應該按照要求工作。

相關問題