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

本小妞迷上赌 提交于 2019-12-31 17:25:53

问题


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 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?


回答1:


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>



回答2:


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.



来源:https://stackoverflow.com/questions/2515954/is-it-possible-to-use-properties-files-in-web-xml-in-conjunction-with-contextco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!