Vlad Mihalcea
Vlad Mihalcea

Reputation: 154020

Spring 4 PropertySourcesPlaceholderConfigurer doesn't resolve ${..} placeholders across modules

I have two modules in my applications:

The core module contains the following property place-holder configuration in the spring/applicationContext-core.xml context:

<bean id="coreProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:/properties/*.properties</value>
            <value>classpath:/profiles/${build.profile.id}/properties/*.properties</value>
            <value>file:${ui.home}/profiles/${build.profile.id}/properties/*.properties</value>
        </list>
    </property>
</bean>

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="false"/>

    <property name="properties" ref="coreProperties" />
</bean>

<bean id="propertySourcesPlaceholderConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

Considering I have the following property:

resource.suffix=.min

If I inject this value in a core @Component:

@Value("${resource.suffix}")
private String resourceSuffix;

The property is properly resolved .

But, if I add the same configuration in a bean inside the web module, which simply loads the core configurations as well:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/servlet-context.xml /WEB-INF/application-security.xml
        classpath*:/spring/applicationContext-core.xml</param-value>
</context-param>

then the property is not resolved and the resourceSuffix value is set to the following String literal value ${resource.suffix.

What am I missing?

Upvotes: 2

Views: 2467

Answers (1)

Mite Mitreski
Mite Mitreski

Reputation: 3626

I believe this has to do with how spring works with pre/post processors.

Basically you can have duplicated definition or use a different mechanism for loading properties.

As much as I know before spring 3.1 duplication is the only way.

More on http://www.baeldung.com/2012/02/06/properties-with-spring/#parent-child

Upvotes: 2

Related Questions