简体   繁体   中英

PropertyPlaceHolderConfigurer not reading Tomcat context XML

I have some properties set in my tomcat context.xml file as follows

<Parameter name="foobar" value="something" />

I am reading these values into my spring XML using the notation ${foobar}. This works fine when I use the context:property-placeholder tag, but when I define this instead as a PropertyPlaceHolderConfigurer bean directly, I get the following error:

Could not resolve placeholder 'foobar' in string value "${foobar}"

Old (working):

<context:property-placeholder location="/WEB-INF/classes/*.properties" />

New (not working):

<bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/classes/app.properties</value>
            <value>/WEB-INF/classes/security.properties</value>
        </list>
    </property>
</bean>

I suspect you are dropping back to this older notation either because you are forced to use an older version of spring or you are wanting to use multiple-locations with context:property-placeholder .

Older versions of spring

When using older versions of spring to get this functionality you should use ServletContextPropertyPlaceholderConfigurer . It doesn't need the servlet container to work as it will fallback to being like PropertyPlaceholderConfigurer . So to adapt your example:

<bean id="propertyPlaceholderConfigurer"
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/classes/app.properties</value>
            <value>/WEB-INF/classes/security.properties</value>
        </list>
    </property>
</bean>

By default the properties defined in the locations take precedence, but this is optional.

Using multiple locations with context:property-placeholder

For newer versions of spring you can use the context xml property-placeholder to load multiple configuration files:

<context:property-placeholder location="/WEB-INF/classes/app.properties" order="1" ignore-unresolvable="true" />
<context:property-placeholder location="/WEB-INF/classes/app.properties" order="2" ignore-unresolvable="true" />

If for some you want to use the more explicit bean for another reason you can use the PropertySourcesPlaceholderConfigurer which is used to implement the xml annotations. But generally people use the xml wiring.

The correct class to use is PropertySourcesPlaceholderConfigurer (as of Spring 3.1).

<bean id="propertyPlaceholderConfigurer"
    class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/classes/app.properties</value>
            <value>/WEB-INF/classes/security.properties</value>
        </list>
    </property>
</bean>

From the JavaDocs:

Specialization of PlaceholderConfigurerSupport that resolves ${...} placeholders within bean definition property values and @Value annotations against the current Spring Environment and its set of PropertySources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM