是否有可能使用.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>
应用-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。
不隶属于 StackOverflow