spring – 是否可以在web.xml中结合contextConfigLocation参数使用.properties文件?
作者:互联网
这是我的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>
或者使用Spring 3.0的EL:
<bean class =
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value = "#{contextParameters.myParameter}" />
</bean>
标签:properties,spring,web-xml 来源: https://codeday.me/bug/20190726/1546529.html