在Spring应用程序中外化web.xml参数

时间:2016-08-22 21:33:11

标签: java spring

我在web.xml文件中的参数很少被外部化。申请是在春季4.0。有没有弹簧的方法来做到这一点?

更准确地说,如果我在上下文文件中定义PropertyPlaceholderConfigurer,有没有办法用它来获取web.xml中的属性?

这就是我的尝试:

在applicationContext.xml中:

function inCategory(category: string, id: string): boolean {
    return id.substring(0, category.length) === category;
}

function favorite(photoId: number): void {
    for (let i = 0; i < categories.length; i++) {
        if (inCategory(categories[i].category.toString(), photoId.toString())) {
            for (let j = 0; j < categories[i].photos.length; j++) {
                if (categories[i].photos[j].id === photoId) {
                    categories[i].photos[j].favorite = true;
                }
            }
        }
    }
}

然后在web.xml中:

<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property
            name="location"
            value="classpath:/test.properties" />
        <property
            name="ignoreUnresolvablePlaceholders"
            value="true" />
    </bean>

但是下面的代码将param值返回为$ {app.url}

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>

1 个答案:

答案 0 :(得分:0)

这是不可能的,因为在上下文初始化之前将加载web.xml。唯一的解决方案是将这些属性移动到特定于应用程序服务器的属性文件。

我正在使用Tomcat,我将此属性移至catalina.properties。现在它正在运作。