本文介绍了是否可以将web.xml中的.properties文件与contextConfigLocation参数一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的web.xml的一部分:

Here is part of my web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:application-config.xml
        </param-value>
</context-param>

application-config.xml使用属性占位符:

application-config.xml uses property placeholder:

<context:property-placeholder location="classpath:properties/db.properties"/>

是否可以通过某种方式定义要在web.xml中而不是application-config.xml中使用的属性文件?

Is it possible somehow to define which properties file to use in web.xml rather than in application-config.xml?

推荐答案

是的,您可以使用ServletContextParameterFactoryBean公开context-param值(它也需要完整形式的PropertyPlaceholderConfigurer而不是简单的context:property-placeholder):/p>

Yes, you can use ServletContextParameterFactoryBean to expose context-param value (it also requires full form of PropertyPlaceholderConfigurer instead of simple context:property-placeholder):

<bean id = "myLocation" class =
    "org.springframework.web.context.support.ServletContextParameterFactoryBean">
    <property name="initParamName" value = "myParameter" />
</bean>

<bean class =
    "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" ref = "myLocation" />
</bean>

或使用Spring 3.0的EL:

Or use Spring 3.0's EL:

<bean class =
    "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value = "#{contextParameters.myParameter}" />
</bean>

这篇关于是否可以将web.xml中的.properties文件与contextConfigLocation参数一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 09:38