2016-02-01 46 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

相关问题