tags:

views:

21

answers:

1

I have three projects - proj-a, proj-b, and main such that main depends on proj-a and proj-b.

proj-a and proj-b each contains a module-context.xml and properties file.

proj-a module-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"&gt;

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

proj-a.properties

property-a=hello-a

proj-b's configuration is identical except a is replaced by b.

proj-b module-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"&gt;

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

proj-b.properties

property-b=hello-b

A Class in main wants to create an ApplicationContext that consists of both proj-a and proj-b's module-context.xml. The problem is that only one of the properties file is processed by spring. If proj-a's module-context.xml's loaded first, proj-b's properties file is never read.

The following snippet throws an Exception.

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"
    } );
}

throws

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)

How can I properly load the properties files? Merging them is not a solution because properties are project specific.

+1  A: 

I found a solution - set property ignoreUnresolvablePlaceholders to true.

Candy Chiu