Announcement Announcement Module
Collapse
No announcement yet.
Hierarchical Application Context Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hierarchical Application Context

    Hi -

    I have a Java EE application where we have the Web layer, the domain layer, the service layer etc. I would like to organize my ApplicationContext in a hierarchy, where some of the beans are common to all layers, while others belong specifically to respective layers. Is there any documented best practice for organizing and implementing the hierarchy ? Any pointers will be appreciated.

    Cheers.
    - Debasish

  • #2
    ApplicationFactory is itself hierarchical (by virtue of being a HierarchicalBeanFactory) and there are plenty of javadocs with those classes. You are already using the hierarchy if you have a ContextLoaderListener and a DispatcherServlet in the same application - this is the most common pattern for combining a web tier with a service tier in the same JVM, so best practice is pretty much the default.

    You can go a small stage further without much effort because the ContextLoaderListener can also load a bean factory as a parent of the main application context (a little used, but well documented feature). If you want more control over the hierarchy, I suggest you write your own ContextLoaderListener using SingletonBeanFactoryLocator (which see).

    Comment


    • #3
      I know, achieve a hierarchy I could use new <>ApplicationContext( "...", parent ), but is it possible to do it from the ctx.xml files? like <import parent="..."/>?

      Comment


      • #4
        The way to do that is through SingletonBeanFactoryLocator, e.g.

        Code:
        <bean id="childKey" lazy-init="true"
            class="org.springframework.web.context.support.XmlWebApplicationContext">
           <property name="configLocations">
             <list>
                 <value>classpath:childContext.xml</value>
             </list>
           </property>
           <property name="parent" ref="parentKey"/>
        </bean>
        If you don't want to use SingletonBeanfactoryLocator directly, you can do this with the normal ContextLoaderListener, but only in the application context layer above the normal web application (the parent and higher).

        Comment


        • #5
          Originally posted by david_syer View Post
          If you don't want to use SingletonBeanfactoryLocator directly, you can do this with the normal ContextLoaderListener, but only in the application context layer above the normal web application (the parent and higher).
          If I use ContextLoadListener, then it loads up all beans (singleton ones) during the web context startup. Here are some confusions ..

          - If I have beans that need to be used further down in the middle layers, say the domain layer or service layer, how should I access that bean ? I do not want to have my middle layers depend on the web layer ..

          - Can I have separate contexts for the middle layer and the web layer, such that the middle layer context is the parent of the web layer context, and the web layer context is loaded up through the ContextLoaderListener, while the parent context through some other ApplicationContext ?

          Thanks.
          - Debasish

          Comment


          • #6
            Can I have separate contexts for the middle layer and the web layer, such that the middle layer context is the parent of the web layer context, and the web layer context is loaded up through the ContextLoaderListener, while the parent context through some other ApplicationContext ?
            Based on the javadoc, ContextLoader (which ContextLoaderListener calls) supports a loadParentContext() method which will be called to locate an application context defined in a beanRefContext.xml file by default. Say you have defined your middle layer context in the beanRefContext.xml with the id "middleLayerCtx" then you should specify a servlet context parameter "parentContextKey" with the value "middleLayerCtx" so that the ContextLoader can pick it up and set is as the parent. More info on beanRefFactory.xml is available in the javadoc for SingletonBeanFactoryLocator.
            Last edited by sabarish; Dec 13th, 2006, 10:32 AM.

            Comment


            • #7
              More details here...

              This technique is explained in detail in this post of Colin's:

              http://forum.springframework.org/sho...58&postcount=2

              Comment


              • #8
                Hi,

                Here is another detailed example for Sharing a spring context across multiple Webapps

                Nabil

                Comment

                Working...
                X