Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override properties from external property file if exists using Spring

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.

like image 915
Saurab Parakh Avatar asked Jan 09 '14 08:01

Saurab Parakh


People also ask

Can we override application properties in spring boot?

To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.

How do you override default properties in spring boot project?

Overriding a Property File Now we'll override properties by putting the property file in the test resources. This file must be on the same classpath as the default one. This method is very effective when we want to override multiple properties from the file. And if we don't put the example.


1 Answers

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.

like image 154
René Link Avatar answered Sep 22 '22 03:09

René Link