开发者

Is it possible to use .properties files in web.xml in conjunction with contextConfigLocation parameter?

开发者 https://www.devze.com 2022-12-24 07:25 出处:网络
Here is part of my web.xml: <context-param> <param-name>contextConfigLocation</param-name>

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-c开发者_运维问答onfig.xml uses property placeholder:

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

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


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>

Or use Spring 3.0's EL:

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


Totally agree with axtavt. So all the info combined the most simple solution with Spring 3.0 thus is:

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

with

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

in web.xml.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号