gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:54:50 29 4
gpt4 key购买 nike

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

最佳答案

我不确定,但是从 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 propertyplaceholder,我们也使用常规 Spring Propertyplaceholder 来执行此操作,但现在找不到示例。

关于java - 为什么 Spring propertyplaceholder 没有检测到我对系统属性的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14361926/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com