2012-06-05 64 views
1

我有一些Spring文件依赖于其他文件中的属性占位符。从本质上讲,我有3个XML文件,像这样:IoC容器属性继承

one.xml

<?xml version="1.0"?> 
<beans ...> 

    <import resource="two.xml"/> 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>/WEB-INF/your.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="clasher" class="whatever.Clash"> 
     <property name="name" value="${route.one}"/> 
    </bean> 
</beans> 

two.xml

<?xml version="1.0"?> 
<beans ...> 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>/WEB-INF/my.properties</value> 
      </list> 
     </property> 
    </bean> 
</beans> 

my.properties

route.one = Why Not? 

your.properties

entirely.unrelated = true 

我越来越基本上看起来像这样的错误:

Exception in thread "Launcher:/serverling" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'clasher' defined in ServletContext resource [/WEB-INF/one.xml]: Could not resolve placeholder 'route.one' 
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:287) 
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:638) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407) 
    at org.red5.server.tomcat.TomcatLoader$1.run(TomcatLoader.java:594) 

有没有一种方法,使财产占位符整个容器继承?

我最初的假设是,由于容器并不关心哪些文件bean存在,它只会配置所有的bean,然后运行通过容器中的所有定义的bean,并使用多个属性占位符配置器来填充它们。

为什么不在此示例workingL

回答

3

设置该标志为PropertyPlaceHolderConfigurer

<property name="ignoreUnresolvablePlaceholders" value="true"/> 
0

,只需提供的PropertyPlaceholderConfigurer的ID(或它们中的至少一个),每个豆科和它应该罚款。我的猜测是,如果没有声明Bean ID,就会覆盖另一个,这就是为什么你失去了一些属性值。如果您在基于注释的文本上下文中使用它,请考虑this issueHere也演示了属性配置器的多个实例的更加精细的使用情况。