我正在编写一个小的UnitTest来确保我的属性回退机制按预期工作。我的想法是,在programmaticaly中在文件系统的某处创建一个属性文件,然后让Spring访问此文件以从这一点加载道具。
问题是,春天看不到/读取文件,即使它存在(Windows资源管理器显示并打开文件)
文件创建:
try {
// Create temp file.
// File file = File.createTempFile("temp", ".properties");
File file = new File("C:/temp/temp.properties");
// den aktuellen Pfad in eine Umgebungsvariable setzen, damit sie von der SpringConfig ausgelesen werden kann.
System.out.println(file.getAbsolutePath());
System.setProperty("tempPropsFilename", file.getAbsolutePath());
// Delete temp file when program exits.
file.deleteOnExit();
Spring config
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- Die Position der Datei in der Liste ist wichtig. Die letzte überschreibt
die erste (natürlich nur die props, die in beiden enthalten sind) -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<array>
<value>${tempPropsFilename}</value>
</array>
</property>
</bean>
来自Spring的错误消息
06-07@09:45:48 [main ] INFO ( PropertiesLoaderSupport.java:177) config.PropertyPlaceholderConfigurer - Loading properties file from class path resource [C:/temp/temp.properties]
06-07@09:45:48 [main ] WARN ( PropertiesLoaderSupport.java:200) config.PropertyPlaceholderConfigurer - Could not load properties from class path resource [C:/temp/temp.properties]: class path resource [C:/temp/temp.properties] cannot be opened because it does not exist
答案 0 :(得分:1)
尝试使用
<property name="location" value="file:/${tempPropsFilename}"/>
它应该使文件系统中的Spring搜索不是类路径。
答案 1 :(得分:0)
据我所知,文件(路径)创建了文件的抽象表示 - 它不会向光盘写入任何内容
如果删除file.deleteOnExit()光盘上是否存在文件?
尝试写入文件 - 我认为会将其提交给光盘。