Override Properties file in Spring WebApp at Runtime

試著忘記壹切 提交于 2019-12-29 09:19:08

问题


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

<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
                <value>classpath:mail.properties</value>
            </list>
        </property>
    </bean>

Now, I want to override some of the properties from mail.properties, so I created an additional entry in my application-context file reading this post, as follows:

<context:property-placeholder location="file:override.properties"
        order="-1" ignore-unresolvable="true" ignore-resource-not-found="true" />

Then, in my Tomcat Server, in VM Arguments of launch configuration, I supplied additional entry:

-Dexternal.props="/Users/ArpitAggarwal/Desktop/override.properties"

With overridden values for some of the keys I have to override.

But, WebApp is not picking up the values from override.properties. Can anybody help me in figuring out, what I am missing.

Any help will be appreciated. Thanks!


回答1:


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.




回答2:


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.




回答3:


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}



来源:https://stackoverflow.com/questions/33184807/override-properties-file-in-spring-webapp-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!