Est-il possible d'utiliser des fichiers .properties dans web.xml conjointement avec le paramètre contextConfigLocation?
-
22-09-2019 - |
Question
Voici une partie de mon web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:application-config.xml
</param-value>
</context-param>
application config.xml utilise la propriété espace réservé:
<context:property-placeholder location="classpath:properties/db.properties"/>
Est-il possible d'une certaine manière de définir les propriétés de fichier à utiliser dans web.xml plutôt que dans l'application-config.xml?
La solution
Oui, vous pouvez utiliser ServletContextParameterFactoryBean
pour exposer la valeur context-param
(il faut aussi pleine forme de PropertyPlaceholderConfigurer
au lieu de context:property-placeholder
simples):
<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>
Ou utiliser Spring 3.0 EL:
<bean class =
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value = "#{contextParameters.myParameter}" />
</bean>
Autres conseils
Totalement d'accord avec axtavt. Donc, toutes les informations combinées de la solution la plus simple avec Spring 3.0 est donc:
<context:property-placeholder location="#{contextParameters.propertiesLocation}"/>
avec
<context-param>
<param-name>propertiesLocation</param-name>
<param-value>classpath:properties/db.properties</param-value>
</context-param>
dans web.xml.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow