2012-06-05 115 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也演示了屬性配置器的多個實例的更加精細的使用情況。