Можно ли использовать файлы .properties в web.xml вместе с параметром contextConfigLocation?
-
22-09-2019 - |
Вопрос
Вот часть моего 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?
Решение
Да, вы можете использовать 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>
Или используйте EL Spring 3.0:
<bean class =
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value = "#{contextParameters.myParameter}" />
</bean>
Другие советы
Полностью согласен с 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>
в веб.xml.
Не связан с StackOverflow