2011-01-31 99 views
1

问候!阅读配置文件出现在弹簧罐内弹出

我有一个弹簧应用程序,它依赖于传统的弹簧应用程序,以jar的形式发货。请注意,遗留的jar在jar本身里面有它的spring配置文件。那么有两个属性文件:app.properties和override.properties。

现在从外项目,我可以使用类似读一个配置属性:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" ref="propertyResource"></property> 
</bean> 

<bean name="propertyResource" class="org.springframework.core.io.ClassPathResource"> 
    <constructor-arg><value>spring-config.properties</value></constructor-arg> 
</bean> 

但我无法让2个属性文件。有人遇到过类似的问题并找到了解决办法吗?请提出建议。我尝试使用propertyResource豆的列表,有两个PropertyPlaceholderConfigurer豆,但没用。我只搜索了(但不是彻底的)春天的文档,所以这将是我将要做的下一件事,但如果有人已经知道这个解决方案,为什么重新发明轮子。

回答

3

如果我理解正确的话,你要加载不止一个属性文件到您的PropertyPlaceholderConfigurer。你可以通过设置'位置'属性来代替'位置'属性。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:app.properties</value> 
      <value>classpath:override.properties</value> 
     </list> 
    </property> 
</bean> 
+0

我试过这个。不知何故,我无法使用此方法加载jar中存在的属性文件。我也尝试使用“classpath *”。 – cheekoo 2011-01-31 17:27:07

2

所有你需要的是忽略了无法解决的占位符属性:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" ref="propertyResource"></property> 
    <property name="ignoreUnresolvablePlaceholders" value="true" /> 
</bean> 

有两点要注意:

看看在p namespace(注:此链接是旧的,但仍然具有现实意义)的简写CONFIGS 。你的配置将成为:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
     p:ignoreUnresolvablePlaceholders="true" 
     p:locations="classpath:spring-config.properties" /> 

而且看一看的context namespace(第C.2.8)。你的配置将成为:

<context:property-placeholder locations="classpath:spring-config.properties" ignore-unresolvable=true"/>