2017-04-25 28 views
1

當我嘗試加載源代碼中的屬性文件時,出現問題。但是當我試圖從外部加載屬性文件時,它工作正常。下面提到了工作代碼和不工作代碼。有人可以幫助我做到這一點。我是春天的一個非常新的蜜蜂:)無法在Spring中通過property-placeholder讀取屬性文件。獲取系統找不到指定的文件錯誤

我從外部和項目中加載屬性文件的唯一更改是上下文中的位置聲明:property-placeholder。

配置彈簧上下文從外部加載屬性文件如下。它工作得很好。

當從外部加載屬性文件 - 工作

<context:property-placeholder location="file:///C:/Test/Data/WebDetails.properties" 
    ignore-resource-not-found="true" ignore-unresolvable="true" order="1" /> 

我配置我的屬性文件從項目如下彈簧背景下加載 - 不工作

<bean id="webProperties" class="com.test.run.WebProperties" /> 

<context:property-placeholder location="file:./src/main/resources/WebDetails.properties" 
    ignore-resource-not-found="true" ignore-unresolvable="true" order="1" /> 

我的WebDetails.properties位於Maven Project下的src/main /資源。而文件看起來像

url = www.testresponse.com 
space = SampleSpace 

,並在網絡媒體資源像下面

@Component 
public class WebProperties{ 
    @Value("{url}") public String url; 
    @Value("{space}") public String space; 

    //getters 
} 

我的主類是我的映射屬性值:

public class ProcessWeb { 
    ApplicationContext context; 
    WebProperties webProperties; 

} 

public ProcessWeb() { 
    context = new ClassPathXmlApplicationContext("web-context.xml"); 
    webProperties= (WebProperties) context.getBean("webProperties"); 
    } 

public void execute(String[] args){ 
    webProperties.getURL() 
} 

public static void main(String args[]) 
{ 
    ProcessWeb main = new ProcessWeb(); 
    main.execute(); 
} 

當我執行我的主類時,從代碼加載性能。我得到的錯誤爲$ {url}(系統找不到指定的文件。)

如何在佔位符中配置我的位置路徑?

更多信息:主要類ProcessWeb將從批處理文件中調用。在部署代碼之後,當我們從命令提示符執行該批處理文件時,我們也得到了與外部屬性相同的問題。是否需要更改任何配置?我們包裝我們的代碼爲jar文件

回答

2

使用classpath像:

location="classpath:WebDetails.properties" 
+0

我嘗試過這一點。從代碼執行時它正在工作。但從命令行來看,它並不工作。 **更多信息:**主類ProcessWeb將從批處理文件中調用。部署代碼後,當我們從命令提示符執行該批處理文件時,會得到相同的問題。是否需要更改任何配置?我們將我們的代碼打包成jar文件。 –

+0

@ User0234 Main類和ProcessWeb都需要WebDetails.properties。或者至少是哪個罐子需要它。檢查你的jar文件的內容。也許屬性文件不存在的jar。 – sfat

相關問題