0

In our application start-up, we are creating one application context having its own bean configuration xml. Then depending up on certain logic, we would need to dynamically load/import the second bean configuration into the existing application context.

One option is to close the existing application context and creating a new application context by importing both the bean definition xml files. But closing the application context takes time ( i.e destroying beans/executors etc ) , is there a way to merge both the application context without closing the existing one.

rockycres
  • 71
  • 8
  • You need to define what 'depending on certain logic' is to clarify this question. Spring Profiles are the generally accepted way of doing conditional beans. – drekbour Mar 22 '22 at 06:33
  • 1
    Do you need the context to execute the logic? Why not execute that before? Another solution would be to use the context you already have as a parent for the other context. – M. Deinum Mar 22 '22 at 11:16
  • @M.Deinum - I was able to fix it by using the parent as the other context. ApplicationContext masterContext = new ClassPathXmlApplicationContext(master,true,aaspireBaseContext); – rockycres Mar 23 '22 at 04:53

1 Answers1

0

When using multiple application contexts you can achieve a hierarchy, which might be helpful here. You then first create the first instance, determine what to load next in a new instance with the first instance as a parent.

Something like this

ApplicationContext parent= new ClassPathXmlApplicationContext("your-context.xml");
String nextXml = logicToDetermineXml();
ApplicationContext toUse = new (new String[] {nextXml}, parent);

Now you can use the one called toUse for the remainder of your application. Without having to recreate the whole context.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224