是否有可能使用.properties文件在web.xml中与contextConfigLocation的参数一起使用?

StackOverflow https://stackoverflow.com/questions/2515954

  •  22-09-2019
  •  | 
  •  

下面是我的web.xml的一部分:

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

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

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

是否有可能以某种方式定义的属性文件,以使用在web.xml而不是在应用程序的config.xml?

有帮助吗?

解决方案

是,可以使用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>

其他提示

共用axtavt同意。因此,所有的信息结合弹簧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。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top