2

How to share parent context with child in spring 5?

Using spring 4, we could pass locatorFactorySelector as context-param

<context-param>
    <param-name>locatorFactorySelector</param-name>
    <param-value>classpath:refFactory.xml</param-value>
</context-param>

This support is removed from Spring 5 onward. What is the alternative to pass the parent context in web context?

Dheeraj Joshi
  • 3,057
  • 8
  • 38
  • 55
  • See [SPR-15154](https://jira.spring.io/browse/SPR-15154) which is the issue that actually removed this. There are some solutions in the discussions. – M. Deinum Jan 08 '19 at 08:53

1 Answers1

3

The loading of the parent context based on locatorFactorySelector were handled at ContextLoader#loadParentContext(). But they changed it to return null in this commit.

As said by the javadoc , I think you can create a new ContextLoaderListener and override this method to return the parent context:

public class FooContextLoaderListener extends ContextLoaderListener{

    @Override
    protected ApplicationContext loadParentContext(ServletContext servletContext) {
        //load and return the parent context ......
    }

}

Then use this ContextLoaderListener to start up Spring :

<listener>
    <listener-class>org.foo.bar.FooContextLoaderListener</listener-class>
</listener>
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • Does extending XmlWebApplicationContext has any limitations? Like public class FooConfigurableWebApplicationContext extends XmlWebApplicationContext { @Override public void setParent(@Nullable ApplicationContext parent) { super.setParent(getContext()); } } and contextClass w.x.y.z.FooConfigurableWebApplicationContext – Dheeraj Joshi Jan 08 '19 at 08:22
  • Do you mean you use `ContextLoaderListener` to start ? but change `context-param` 's `contextClass` to `FooConfigurableWebApplicationContext`? – Ken Chan Jan 08 '19 at 08:26
  • Yes. Use default listener but change context class. – Dheeraj Joshi Jan 08 '19 at 08:29
  • briefly look at the source codes and seem it will not work. Although your `FooConfigurableWebApplicationContext#setParent(parent)` will be invoked , but the parent parameters is always loaded from the method that I suggest you to override...If you do not override , it will always be `null`.. – Ken Chan Jan 08 '19 at 08:48
  • Ok. I moved ahead with this problem. Now it appears velocity is also removed from spring 5. And i have no clue how to give velocity view resolver and velocity configurer bean definitions. Its totally different problem. But i hoped spring team would give an alternative or examples for legacy products. – Dheeraj Joshi Jan 08 '19 at 12:45
  • For future reference: the above mentioned commit was part of the following feature: "Drop outdated BeanFactoryLocator / beanRefContext.xml mechanism" https://github.com/spring-projects/spring-framework/issues/19720 – MyKey_ Apr 27 '21 at 10:27
  • DheerajJoshi and @MyKey_ We are still facing the same issue with the above mentioned workaround. Please refer https://stackoverflow.com/questions/71365412/not-able-to-refer-to-different-xml-using-spring-5-2-x. Can you please help us. – Subhranil Sengupta Mar 07 '22 at 08:48
  • We had to change the custom spring bean factory to get this working.......... public class CustomContextLoaderListener extends ContextLoaderListener { @Override public ApplicationContext loadParentContext(ServletContext servletContext) { return CustomSpringBeanFactory.getApplicationContext(); } } – Dheeraj Joshi Mar 21 '22 at 09:11