Override Properties file in Spring WebApp at Runtime

前端 未结 3 1479
耶瑟儿~
耶瑟儿~ 2020-12-22 04:57

I am loading properties file in my Spring WebApplication using PropertyPlaceholderConfigurer as below:



        
相关标签:
3条回答
  • 2020-12-22 05:21

    you just use in your application-context

    <context:property-placeholder location="file:///${external.props}"
        order="-1" ignore-unresolvable="true" ignore-resource-not-found="true" />
    

    problem is you just wrongly used location, actual location is vm arg variable key => ${external.props}

    0 讨论(0)
  • 2020-12-22 05:26

    What I did to resolve the problem is, replaced the location attribute as ${ext.properties.dir:classpath:}/override.properties, as follows:

    <context:property-placeholder
            location="${ext.properties.dir:classpath:}/override.properties" order="-1"
            ignore-unresolvable="true" ignore-resource-not-found="true" />
    

    And supplied ext.properties.dir value from application-server/jvm as:

    -Dext.properties.dir=file:/Users/ArpitAggarwal/properties/
    

    Reference: 6-tips-for-managing-property-files-with-spring.

    0 讨论(0)
  • 2020-12-22 05:29

    I think that working with multiple propertyPlaceholders is not a goog idea. I had a lot of problems when two xml configs, in the same context, loads each properties and they try cross-use them.

    If you want to externalize a properties file, I would suggest a JNDI property:

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
                <value>classpath:mail.properties</value>
                <value>${java:com/env/myApp/externalProperties}
            </list>
        </property>
    </bean>
    

    Where the value of this JNDI would be: "file:/path-to-propertiesFile".

    In this way you only need one propertyePlaceholder. Also note that by putting the external file as last location, if you have duplicate keys, spring will retain only the last one.

    0 讨论(0)
提交回复
热议问题