Can I use .properties files in web.xml in combination with the contextConfigLocation parameter?

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 the placeholder property:

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

Is it possible to somehow determine which property files to use in web.xml, and not in application-config.xml?

+3
source share
2 answers

Yes, you can use ServletContextParameterFactoryBeanvalues ​​to expand context-param(it also requires a full form PropertyPlaceholderConfigurerinstead of a simple one 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 EL:

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

I completely agree with axtavt. Thus, all the information combines the simplest solution with Spring 3.0, thus:

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

+4

Source: https://habr.com/ru/post/1738492/


All Articles