2010-07-10 40 views
0

我有三個項目 - proj-a,proj-b和main,使得主要依賴於proj-a和proj-b。從工作區中的項目加載多個屬性文件

proj-a和proj-b每個都包含一個module-context.xml和properties文件。

PROJ-一個模塊的context.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:property-placeholder location="classpath:/META-INF/proj-a.properties"/> 
    <bean ... someProperty="${property-a}" /> 
</bean> 

proj-a.properties

property-a=hello-a 

PROJ-B的配置是除外相同由b取代。

PROJ-B模塊的context.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <context:property-placeholder location="classpath:/META-INF/proj-b.properties"/> 
    <bean ... someProperty="${property-b}" /> 
</bean> 

proj-b.properties

property-b=hello-b 

在主A類想要創建由兩個凸出-a和proj-的一個ApplicationContext b的module-context.xml。問題是隻有一個屬性文件是由spring處理的。如果首先加載proj-a的module-context.xml,則永遠不會讀取proj-b的屬性文件。

以下片段引發異常。

public static void main(String[] args) throws IOException { 

    ApplicationContext context = new FileSystemXmlApplicationContext(new String[] { 
      "../proj-a/src/main/resources/META-INF/spring/module-context.xml", 
      "../proj-b/src/main/resources/META-INF/spring/module-context.xml" 
    }); 
} 

拋出

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name '...' defined in file [...\proj-b\src\main\resources\META-INF\spring\module-context.xml]: Could not resolve placeholder 'property-b' 
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:272) 
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75) 
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:624) 
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:599) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:398) 

我怎樣才能正確加載屬性文件?合併它們不是一個解決方案,因爲屬性是項目特定的。

回答

1

我發現了一個解決方案 - 將屬性ignoreUnresolvablePlaceholders設置爲true。

相關問題