Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Framework 5.0.0.Final parent context not getting loaded

I was trying with latest spring 5.0.0.Final with my EAR project which has a parent context defined in web.xml using context-param with param names locatorFactorySelector and parentContextKey but spring could not able to load the parent context. When i checked the ContextLoaderListener source code it seems like there is no logic applied to pick parent context. Here my question is does spring 5 provide any default implementation of ContextLoader which caters loading of parent context or spring 5 dropped, if not what is the approach to support this, do i have to implement our own ?

like image 911
Sreekanth Avatar asked Oct 20 '25 22:10

Sreekanth


2 Answers

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>

For me this below piece of code worked fine.

public class BeanFactoryContextLoaderListener extends ContextLoaderListener {
    
    private static Logger log = Logger.getLogger(BeanFactoryContextLoaderListener.class);
    
     @Override
        protected ApplicationContext loadParentContext(ServletContext servletContext) {
            
         ApplicationContext ctx = new ClassPathXmlApplicationContext("beanRefFactory.xml");
         
         return ctx;
        }

}

Obviously I added a listener too in web.xml.

like image 184
Subhranil Sengupta Avatar answered Oct 22 '25 19:10

Subhranil Sengupta


My team recently bumped into the same problem. We wanted to start using Webflux and it requires Spring 5. Here is what I did:

  1. Manually reintroduce BeanFactoryLocator mechanism. Take following classes from Spring 4, put it into your code and fix packages:
AbstractUrlMethodNameResolver
AnnotationMethodHandlerAdapter
BeanFactoryLocator
BeanFactoryReference
BootstrapException
ContextSingletonBeanFactoryLocator
DefaultAnnotationHandlerMapping
HandlerMethodInvocationException
HandlerMethodInvoker
HandlerMethodResolver
InternalPathMethodNameResolver
MethodNameResolver
NoSuchRequestHandlingMethodException
ServletAnnotationMappingUtils
SingletonBeanFactoryLocator
SourceHttpMessageConverter
WebUtils
XmlAwareFormHttpMessageConverter
  1. Following Subhranil's advice from this thread, use custom ContextLoaderListener which loads parent context same as in Spring 4. Then use it in web.xml.
  2. In each WAR's spring-servlet.xml add DefaultAnnotationHandlerMapping so it scans for controllers. Accompanying beans like AnnotationMethodHandlerAdapter are also needed.

It worked for us.

like image 25
Marcin Cieslak Avatar answered Oct 22 '25 19:10

Marcin Cieslak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!