views:

595

answers:

3

Hello,

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.

+1  A: 

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).

Michael Wiles
Whatever u said is fine. But I need some usage on how to do that. What configurations are required etc. ("How" is important here )
peakit
+1  A: 

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.

skaffman
Skaffman, do we have something for non-webapps also?And secondly, could you please point me to sample web.xml where hierarchy is being set?
peakit
+2  A: 

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.

peakit