【发布时间】:2013-07-19 02:43:46
【问题描述】:
我正在使用系统属性来定义特定于环境的属性文件的位置。但是,我想将该值覆盖为与集成测试不同的值。
这是我的生产弹簧设置。我正在使用自定义 PropertyPlaceholderConfigurer 来解析一些加密的属性文件值,但这在这里并不重要:
<-- Spring configuration in file service-spring-beans.xml -->
<bean class="com.mycompany.MyPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/${MY_ENVIRONMENT}/${MY_ENVIRONMENT}.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
在运行时,我们将 MY_ENVIRONMENT 的值定义为 Java 系统属性。这一切都按预期工作。但是,对于集成测试,我想将 MY_ENVIRONMENT 定义为“inttest”,因此会加载集成测试特定的属性文件 properties/inttest/inttest.properties。
我尝试使用集成测试加载的 spring 上下文来设置一个 ID 为 MY_ENVIRONMENT 的 String bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.mycompany.myclasses"/>
<bean class="java.lang.String" id="MY_ENVIRONMENT">
<constructor-arg value="inttest"/>
</bean>
<!-- this imports the production spring context -->
<import resource="classpath:service-spring-beans.xml"/>
</beans>
但是,MY_ENVIRONMENT 的值未解析,运行集成测试时出现此错误。
原因: org.springframework.beans.factory.BeanInitializationException:可以 不加载属性;嵌套异常是 java.io.FileNotFoundException:类路径资源 [properties/${MY_ENVIRONMENT}/${MY_ENVIRONMENT}.properties] 不能 因为不存在所以打开
如何在不将系统属性传递给 JVM 的情况下在测试时覆盖 MY_ENVIRONMENT?
【问题讨论】:
标签: spring