2013-01-16 31 views
1

我想控制使用Spring的屬性佔位符加載屬性文件並設置系統屬性。這就像它應該:爲什麼Spring的propertyplaceholder沒有檢測到我對系統屬性的更改?

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

和設置系統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等,我沒有得到?

回答

0

我不確定,但沒有設置系統屬性從一個Spring上下文文件還沒有太晚?我們使用類似的設置,但不是改變系統屬性測試我們使用的測試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> 

在真實的情況下。

在這裏忽略我使用駱駝propertyplaceholder,我們也使用普通的Spring Propertyplaceholder來做到這一點,但現在找不到示例。

+0

是的,我結束了使用類似的解決方案。我仍然想知道爲什麼原始解決方案不起作用。如何設置系統屬性爲時已晚?所以我沒有把這個答案標記出來。 –

+0

我猜想,在Spring開始解析它的上下文文件之前,它需要當時聲明的所有環境變量。上下文:placeholder bean將在更改環境屬性的bean之前處理,或者該命令不是確定性的。 –

相關問題