以下是我的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中?
答案 0 :(得分:4)
是的,您可以使用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>
答案 1 :(得分:4)
完全同意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>
在web.xml中。