Spring: overriding properties in child context at runtime

In the parent context, I have a property declaration as follows:

<bean id="my.properties" class="com.rcslabs.webcall.server.property.PropertyPaceholderConfigurer"> <property name="locations" value="classpath:/my.properties"/> </bean> 

After that, at runtime, I need to create a child context and override these properties with runtime data. What is the best way to do this?

Addition:

To be more precise, I create the child context manually at runtime as follows:

 ClassPathXmlApplicationContext childAppContext = new ClassPathXmlApplicationContext(parentApplicationContext); 

So, can I declare a bean in childAppContext, as is usually done using BeanDefinitionRegistry?

+4
source share
2 answers

It looks like you have a subclass of PropertyPlaceholderConfigurer, why don't you override resolveProperty with a logical check of the run-time values โ€‹โ€‹and resolveProperty to the default values โ€‹โ€‹otherwise? You may need to create a dedicated subclass for the child context and enter the source of the run-time values โ€‹โ€‹in it.

What you can also do is put your runtime values โ€‹โ€‹in the system properties and use the override mode for systemPropertiesMode . This is a simple but not very clean solution; some of the options for my first approach would be better. If you create multiple click contexts, this will work until you create them in parallel.

update: I would start with something like:

 final Map<String,String> myRuntimeValues; ClassPathXmlApplicationContext childAppContext = new ClassPathXmlApplicationContext(parentApplicationContext) { protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.prepareBeanFactory(); beanFactory.registerSingleton("myRuntimeValues", myRuntimeValues); } }; 

and enter "myRuntimeValues" in the PropertyPlaceholderConfigurer bean defined in the client context file. Some more digging may lead to a better solution, this is not a typical use case, I'm sure you will understand further.

+1
source

Developing mrembisz's answer, here is a complete example for dynamically injecting properties into a spring context without hard coding any bean inside the child xml and then passing the bean link from the parent context. The solution below does not need to define a parent context for this purpose.

 public static void main(String args[]) { AbstractApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] { "classpath:spring-beans.xml" }) { protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { super.prepareBeanFactory(beanFactory); ArrayList<Properties> prList = new ArrayList<Properties>(); Properties pr = new Properties(); pr.setProperty("name", "MyName"); prList.add(pr); Properties prArray[] = new Properties[prList.size()]; PropertySourcesPlaceholderConfigurer pConfig = new PropertySourcesPlaceholderConfigurer(); pConfig.setPropertiesArray(prList.toArray(prArray)); beanFactory.registerSingleton("pConfig", pConfig); } }; appContext.refresh(); System.out.println(appContext.getBean("name")); } 
0
source

Source: https://habr.com/ru/post/1390629/


All Articles