首页 > 解决方案 > 在 contextConfigLocation 参数中配置的 xml 中替换占位符的问题

问题描述

当我将占位符放入 web.xml 时,它会被 catalina.properties 中定义的值或 vm 选项中的 -D 参数正确替换。但是当我将占位符放入由 ContextLoaderListener 加载的 xml 时,它不起作用。

web.xml 片段:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        ${placeholder}, <!-- WORKS -->
        /WEB-INF/applicationContext.xml <!-- DON'T WORK, WHEN PLACEHOLDER IS DEFINED INSIDE XML -->
    </param-value>
</context-param>

标签: javaspringtomcat

解决方案


问题解决了。contextConfigLocation 中定义的xml由spring加载,需要从org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类中定义bean。属性文件可以为空。值将从 catalina.properties 或 vm 选项加载。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:example.properties</value>
    </property>
</bean>

推荐阅读