3

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

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

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

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

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

4

2 回答 2

4

是的,您可以使用ServletContextParameterFactoryBean来公开context-param价值(它还需要完整的形式PropertyPlaceholderConfigurer而不是简单的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:

<bean class = 
    "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value = "#{contextParameters.myParameter}" />
</bean>
于 2010-03-25T14:20:17.860 回答
4

完全同意axtavt。因此,所有信息将最简单的解决方案与 Spring 3.0 相结合,因此是:

<context:property-placeholder location="#{contextParameters.propertiesLocation}"/>

<context-param>
   <param-name>propertiesLocation</param-name>
   <param-value>classpath:properties/db.properties</param-value>
</context-param>

在 web.xml 中。

于 2010-03-26T12:18:54.700 回答