2010-07-10 38 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。

相关问题