Question

Has anyone tried loading multiple hierarchical contexts (parent/child) using Spring?

Could you explain how to do that as I could not locate enough documentation on this?

Thanks in advance.

Was it helpful?

Solution

The Spring classes ContextLoaderListener and ContextLoader perform the task of initializing the webapp's application context, as well as optionally associating another application context as the parent.

In particular, the loadParentContext() method by default uses ContextSingletonbeanFactoryLocator to try and find the webapp's parent context, but if that's not suitable then you can subclass ContextLoader (and ContextLoaderListener) to programmatically locate your library's appcontext.

OTHER TIPS

Seems like I have figured out how to do this.. (thanks to 'skaffman' for helping)

In the web.xml define a context param locatorFactorySelector and set it's value to parent context's beans.xml file. By default, the search is for classpath*:beanRefContext.xml file.

Thats it !

Now I need to see how to make my JUnits load the parent context. Should be simple - by just placing the beanRefContext.xml file in the same directory but lets see.

Create your application context with the 3rd party application context as a parent. That way the 3rd party app context will not be able to see your beans but your application context will be able to see the beans created by the 3rd party library (it's parent factory).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top