2016-02-01 48 views
0

我有一個項目的META-INF文件夾中的應用程序上下文文件。applicationContext不加載屬性是一些文件的路徑

的目錄樹:

├── src 
   ├── main 
   │   . 
     . 
     . 
     . 
     . 

   └── test 
    ├── java 
      . 
      . 
      . 
      . 
      . 

    └── resources 
     ├── META-INF 
     │   ├── applicationContext.annotation.config.xml 
     │   ├── applicationContext.annotationTestCase.config.xml 
     │   └── applicationContext.xml 
     │ 
     ├── annotation.properties 
     ├── annotationTestCase.properties 
     ├── query.properties 
     └── project.properties 

applicationContext.xml我有一個屬性:

<property name="propFile" value="annotation.properties"/> 

我喜歡private String propFile;一個java文件中使用這個屬性。 propFile基本上是加載到程序中的屬性文件的路徑。但只要我打開我得到:

java.lang.IllegalStateException: Failed to load ApplicationContext 
    Caused by: java.nio.file.NoSuchFileException: annotation.properties 

我還印着這是annotation.propertiespropsFile值。

什麼可能會出錯?

+0

的路徑是在類路徑中,而不是一個普通的文件,我建議使用溫泉資源抽象載入的文件,而不是自己動手。 –

回答

0

它只是不是文件名,請檢查您嘗試讀取的文件路徑。

// From ClassLoader, all paths are "absolute" already - there's no context 
// from which they could be relative. Therefore you don't need a leading slash. 
    InputStream in = this.getClass().getClassLoader() 
          .getResourceAsStream("annotation.properties"); 

這不是很好的編碼,但使用彈簧代替。如果你想要的名稱爲「annotation.properties」是動態的PropertyPlaceHolderConfigurer加載任何屬性文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>annotation.properties</value> 
    </property> 
</bean> 

,然後從系統屬性通過。

的Java -DpropertyFilePath = /路徑/要/ propertyfile

相關問題