29

I have the following property file defined in one of my Spring configuration file:

<context:property-placeholder location="classpath:project.properties"/> 

Now I want to override few properties from some external property file that is not in the classpath.

Let's say I have my project deployed somewhere and I need to some dynamic configuration change. I do not want to make updates to the project codebase in the container(tomcat or any thing).

1.) So I need a way that updates (overrides) the values of spring's loaded properties file with my recent updates in the external property file.

2.) It would be great if somebody could also share the way to refresh the properties that are preloaded.

0

2 Answers 2

36

So I need a way that updates (overrides) the values of spring's loaded properties file with my recent updates in the external property file.

You can use the PropertyPlaceholderConfigurer.

Either this way If you want to use the context namespace

<context:property-placeholder location="classpath:yourClasspath.properties,file:/some/resource/path/filePropertiesToOverride.properites"/> 

or this way

<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:yourClasspath.properties</value>
            <value>file:/some/resource/path/filePropertiesToOverride.properites</value>
        </list>
    </property>
</bean>

According to the javadoc of PropertiesLoaderSupport.setLocations(Resource[])

... Note: Properties defined in later files will override properties defined earlier files, in case of overlapping keys. Hence, make sure that the most specific files are the last ones in the given list of locations.

.

It would be great if somebody could also share the way to refresh the properties that are preloaded.

At the moment you are using a PropertyPlaceholderConfigurer. Since a PropertyPlaceholderConfigurer is a BeanFactoryPostProcessor it traverses the bean definitions (object representation of the beans.xml) and replaces the property strings (such as ${someProp}). After that the beans get instantiated and initialized. Thus there is no way to 'reload' the properties.

There is even more to consider if you want to build an application that can react to property changes at runtime:

  • How can you trigger the change at Runtime? E.g. a timer that polls a property file for changes, JMX, ...?
  • How are classes that depend on properties are informed about the update? E.g. a listener implementation.
  • How do I synchronize the updates of many dependent properties? E.g. imagine what will happen if properties get updated during a web app request without synchronization. A part of the request might use the old and another the new properties.

At least I would recommend to use apache commons configuration. But it is only a framework that solves a few problems and you still have to think about solutions to the questions above.

3
  • How will the properties get refreshed on the fly if I make changes to the external property file? Commented Jan 9, 2014 at 9:36
  • 2
    @SaurabParakh dynamically changing properties is a big effort, because there are a lot of things to consider. I updated my answer and hope it helps you to make a decision.
    – René Link
    Commented Jan 9, 2014 at 10:19
  • @RenéLink is a solution possible with annotations and possibly an environment variable? TIA Commented Mar 14, 2017 at 11:53
0

in spring boot 2, it is

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

You can get full details in https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.