هل من الممكن استخدام ملفات .properties في web.xml بالتزامن مع معلمة ContextConfigLocation؟

StackOverflow https://stackoverflow.com/questions/2515954

  •  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>

أو استخدم SPRING 3.0's EL:

<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>

في web.xml.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top