为什么Spring propertyplaceholder没有检测到我对系统属性的更改?

时间:2013-01-16 15:29:54

标签: java spring configuration

我想使用Spring的property-placeholder控制属性文件的加载并设置系统属性。这应该是这样的:

  <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>

但春天不接受该财产:

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等的东西,我没有得到?

1 个答案:

答案 0 :(得分: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>       

VS

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

在真实环境中。

请在此处忽略我正在使用Camel propertyplaceholder,我们也使用常规Spring Propertyplaceholder执行此操作,但现在无法找到示例。