2011-09-29 51 views
1

我有下面的bean定义。如果我将“exposeSystemProperties”bean的placeholderPrefix更改为“$ {”,并在第二个bean的属性路径中使用它,它将起作用。如果我将其更改为任何内容,但“%{”不起作用。我不能使用任何其他字符串(例如“$ sys {”,“#[”等)。我目前在3.0.5.RELEASE上。为什么Spring 3.x忽略PropertyPlaceholderConfigurer的某个placeholderPrefixes?

任何想法,为什么这是?为了复合这一切,我有第三个PropertyPlaceHolderConfigure,所以只有两个前缀不起作用。

<bean id="exposeSystemProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> 
    <property name="placeholderPrefix"><value>$sys{</value></property> 
    <property name="order" value="10" /> 
    </bean> 

    <bean id="localFileProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" /> 
    <property name="placeholderPrefix" value="%{" /> 
    <property name="placeholderSuffix" value="}" /> 
    <property name="order" value="20" /> 
    <property name="locations"> 
     <array> 
     <bean class="java.lang.String"> 
      <constructor-arg><value>classpath:properties/$sys{deploy.env}/client.properties</value></constructor-arg> 
     </bean> 
     </array> 
    </property> 
    </bean> 

回答

2

因为你所需要的前缀是控制环境的具体性能,这可以通过(在你的榜样,而不是一个deploy.env财产的)通过系统变量进行:

<value>classpath:properties/${ENV_SYSTEM:dev}/client.properties</value> 

在这种情况下,它总是会看起来如下:

<value>classpath:properties/dev/client.properties</value> 

默认情况下,除非一个ENV_SYSTEM系统变量已设置。如果它被设置为“质量保证”,例如,它会自动寻找下:

<value>classpath:properties/qa/client.properties</value> 

另一种方法,如果你是开放的“展望未来”了一下,是使用Spring 3.1的PROFILE feature,其中bean可以是特定配置文件。例如:

<beans profile="dev"> 
    <jdbc:embedded-database id="dataSource"> 
     <jdbc:script location="classpath:com/bank/config/sql/schema.sql"/> 
     <jdbc:script location="classpath:com/bank/config/sql/test-data.sql"/> 
    </jdbc:embedded-database> 
</beans> 

dataSource只会在情况下加载的配置文件设置为dev

GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); 
ctx.getEnvironment().setActiveProfiles("dev"); 
ctx.load("classpath:/org/boom/bang/config/xml/*-config.xml"); 
ctx.refresh(); 
+0

所以你说春天负荷高达系统变量的情况下?我以为你需要另辟蹊径。 –

+0

@Spencer,是的:在应用程序上下文创建时可以使用'System' /'Environment'变量,因此您可以依靠它们来驱动环境。这些变量可以用多种方式设置,例如:'System.setProperty(“ENV_SYSTEM”,“qa”)'或者你可以简单地通过'-D' JVM参数传递:'-DENV_SYSTEM = qa' – tolitius

+0

这当然有效围绕我的问题,但仍不一定解决配置器不能正确解析的问题。我没有时间实际调试这个内联,但是当我这样做时,这是我列表中的第一件事。感谢不同的观点。 –