我在项目的META-INF
文件夹中有一个应用程序上下文文件。
目录树:
├── 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
我还打印了propsFile
的值annotation.properties
。
可能出现什么问题?
答案 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");
这不是很好的编码,而是使用spring代替。 PropertyPlaceHolderConfigurer加载任何属性文件
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>annotation.properties</value>
</property>
</bean>
如果你想要名称&#34; annotation.properties&#34;是动态的,然后从系统属性传递它。
java -DpropertyFilePath = / path / to / propertyfile