2013-07-30 16 views
0

我有一些特性在我的Tomcat context.xml文件设置如下PropertyPlaceHolderConfigurer不读Tomcat相关XML

<Parameter name="foobar" value="something" /> 

我使用的符号$ {} foobar的读取这些值代入我的春天XML。这工作得很好,当我使用的背景:财产占位符标记,但是当我直接定义这个代替作为提供一个PropertyPlaceholderConfigurer豆,我得到以下错误:

Could not resolve placeholder 'foobar' in string value "${foobar}"

旧(工作):

<context:property-placeholder location="/WEB-INF/classes/*.properties" /> 

新(不工作):

<bean id="propertyPlaceholderConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>/WEB-INF/classes/app.properties</value> 
      <value>/WEB-INF/classes/security.properties</value> 
     </list> 
    </property> 
</bean> 
+0

看起来不错,以me..Please发布详细的异常。此外,尝试清理你的tomcat和你的项目只是为了确保更新已被正确采用 –

回答

1

我怀疑你是掉到了这个老的表示形式或者是因为你不得不使用Spring的旧版本或者您想要使用多LOCAT离子与context:property-placeholder

春天

的旧版本在使用旧版本的春天得到这个功能,您应该使用ServletContextPropertyPlaceholderConfigurer。它不需要servlet容器工作,因为它会回退到PropertyPlaceholderConfigurer。因此,要适应你的例子:

<bean id="propertyPlaceholderConfigurer" 
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>/WEB-INF/classes/app.properties</value> 
      <value>/WEB-INF/classes/security.properties</value> 
     </list> 
    </property> 
</bean> 

默认情况下,在位置定义的属性的优先级,但这是可选的。

使用多个位置与context:property-placeholder

因为春天的新版本,你可以使用上下文xml属性占位符加载多个配置文件:

<context:property-placeholder location="/WEB-INF/classes/app.properties" order="1" ignore-unresolvable="true" /> 
<context:property-placeholder location="/WEB-INF/classes/app.properties" order="2" ignore-unresolvable="true" /> 

如果出于某些要使用由于另一个原因,您可以使用用于实现xml注释的PropertySourcesPlaceholderConfigurer更明确的bean。但通常人们使用xml布线。

+0

ServletContextPropertyPlaceholderConfigurer已被弃用,但我发现我需要使用PropertySourcesPlaceholderConfigurer – acvcu

+0

如果您使用的Spring版本有PropertySourcesPlaceholderConfigurer为什么你要离开? – andygavin

+0

我需要设置一个自定义的PropertiesPersister来使用,而且我没有看到使用context:property-placeholder notation来做这件事的方法。 – acvcu

1

正确使用的类是PropertySourcesPlaceholderConfigurer(从Spring 3.1开始)。

<bean id="propertyPlaceholderConfigurer" 
    class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>/WEB-INF/classes/app.properties</value> 
      <value>/WEB-INF/classes/security.properties</value> 
     </list> 
    </property> 
</bean> 

从JavaDoc中:

Specialization of PlaceholderConfigurerSupport that resolves ${...} placeholders within bean definition property values and @Value annotations against the current Spring Environment and its set of PropertySources.

相关问题