1

我想使用 Spring 的属性占位符控制属性文件的加载并设置系统属性。这就像它应该的那样工作:

  <context:property-placeholder
location="classpath:jdbc/jdbc.${TARGET_ENV}.properties" />

当我在不同的环境中运行我的应用程序时,我设置了系统属性 TARGET_ENV 并拾取了正确的属性文件。

现在我进行了集成测试,我想强制配置始终加载特定的属性文件(jdbc.INTEGRATION_TESTS.properties)。

我有

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:/META-INF/spring/set-system-property.xml", 
    "classpath:/META-INF/spring/applicationContext.xml"}) 
public class AbstractIntegrationTest extends AbstractTransactionalJUnit4SpringContextTests {

和 set-system-property.xml (使用来自Set System Property With Spring Configuration File的建议):

<beans>
<bean id="systemPrereqs"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" value="#{@systemProperties}" />
    <property name="targetMethod" value="putAll" />
    <property name="arguments">
        <util:properties>
            <prop key="TARGET_ENV">INTEGRATION_TESTS</prop>
        </util:properties>
    </property>
</bean>

但是Spring没有收到该物业:

Caused by: java.io.FileNotFoundException: class path resource [jdbc/jdbc.${TARGET_ENV}.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)

为什么这不起作用?我没有得到关于 bean 实例化、工厂 bean 等的东西吗?

4

1 回答 1

0

我不确定,但是从 Spring 上下文文件中设置系统属性还不算太晚吗?我们使用类似的设置,但我们使用 test-context.xml 来代替更改测试的系统属性,它使用固定路径来加载属性

例如在测试中:

    <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:be/xxx/broker/standalone-test.properties</value>
        </list>
    </property>   
</bean>       

对比

    <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>${BROKER_CONF}</value>
        </list>
    </property>   
</bean>

在真实的环境中。

在这里忽略我正在使用 Camel 属性占位符,我们也使用常规的 Spring 属性占位符执行此操作,但现在找不到示例。

于 2013-01-16T15:34:15.637 回答