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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With