簡體   English   中英

在類路徑之外訪問屬性文件

[英]Access a Property file outside the classpath

我需要在類路徑之外訪問屬性文件。 文件路徑將位於Windows C:\\Temp\\remote.txt或linux /tmp/remote.txt 啟動應用程序服務器時,我的spring解決方案如何訪問此文件並讀取內容。

如何使用Spring PropertyPlaceholderConfigurer或Spring Hibernate應用程序中的任何其他機制執行此操作

一個例子:

<context:property-placeholder location="file:/E:/Workspace/123.properties" />

PropertyPlaceholderConfigurer

從Spring 3.1開始,應優先使用PropertySourcesPlaceholderConfigurer ,而不要使用此實現。 通過利用Spring 3.1中也提供的EnvironmentPropertySource機制,它更加靈活。

使用PropertySourcesPlaceholderConfigurer可以通過以下方式從文件系統上的任何路徑加載屬性:

PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

configurer.setLocations(new Resource[] {
    new FileSystemResource(fileName),
});

您可以使用get資源作為流,例如:

String filePath = "C:/Temp/remote.txt";

BufferedReader input = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(filePath)));

希望對您有所幫助

暫無
暫無

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

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