Java 如何在Junit中为Spring PropertyPlaceHolderConfigure创建临时属性文件
我正在编写一个小型单元测试,以确保我的属性回退机制按预期工作。我的想法是,通过编程,我在文件系统的某个地方创建一个属性文件,然后让Spring访问这个文件,从这一点加载道具 问题是,spring没有看到/读取文件,即使它在那里(Windows资源管理器显示并打开文件) 文件创建n:Java 如何在Junit中为Spring PropertyPlaceHolderConfigure创建临时属性文件,java,spring,io,Java,Spring,Io,我正在编写一个小型单元测试,以确保我的属性回退机制按预期工作。我的想法是,通过编程,我在文件系统的某个地方创建一个属性文件,然后让Spring访问这个文件,从这一点加载道具 问题是,spring没有看到/读取文件,即使它在那里(Windows资源管理器显示并打开文件) 文件创建n: try { // Create temp file. // File file = File.createTempFile("temp", ".properties");
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();
弹簧配置
<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>
就我所记得的,文件(路径)创建了文件的抽象表示——它不向光盘写入任何内容
如果删除文件。deleteOnExit()该文件是否存在于光盘上
尝试写入文件-我认为这会将其提交到光盘。据我记忆所及,文件(路径)创建了文件的抽象表示-它不会将任何内容写入光盘
如果删除文件。deleteOnExit()该文件是否存在于光盘上
尝试写入文件-我想这会将其提交到光盘。尝试使用
<property name="location" value="file:/${tempPropsFilename}"/>
它应该使文件系统中的Spring搜索不是类路径
尝试使用
<property name="location" value="file:/${tempPropsFilename}"/>
它应该使文件系统中的Spring搜索不是类路径
您确定属性文件是在加载Spring上下文之前创建的吗?是的。文件创建部分位于BeforeClass方法中。当我意识到它不起作用时,我将创建文件的代码移到了初始化Spring AppContext的同一个例程中。你确定属性文件是在加载Spring context之前创建的吗?是的。文件创建部分位于BeforeClass方法中。当我意识到它不起作用时,我将创建文件的代码移到了初始化Spring AppContext的同一个例程中,我已经插入了所有必要的配置值。当我使用Windows资源管理器查看路径时,我可以看到包含其内容的文件。c:\temp在类路径上?错误消息说“无法从类路径资源加载属性”。我已经插入了所有必要的配置值。当我使用Windows资源管理器查看路径时,我可以看到包含其内容的文件。c:\temp在类路径上?错误消息说“无法从类路径资源加载属性”就是这样。。。非常感谢,就这样。。。非常感谢你。