Results 1 to 3 of 3

Thread: How to create parent/child contexts in a declarative way?

  1. #1

    Default How to create parent/child contexts in a declarative way?

    Suppose I have a Spring web application. I have two context files:
    applicationContext1.xml and applicationContext2.xml.
    They are loaded into the system as follows:

    web.xml:
    ...
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/applicationContext1.xml
    /WEB-INF/applicationContext2.xml
    </param-value>
    </context-param>
    ...

    Based on the two files, I want to create two contexts
    with a child/parent relationship. Can I?
    How can I make one context is the child of the other one
    in a declarative way?

    Thanks,
    Pete

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    This is quite easy using the built in optional loading of a parent context to the main web app context, that the ContextLoader can do.

    Code:
    web.xml&#58;
    ...
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
          /WEB-INF/applicationContext1.xml
        </param-value>
      </context-param>
    
      <!--  load a shared service layer parent application context -->
      <context-param>
        <param-name>locatorFactorySelector</param-name>
        <param-value>beanRefContext.xml</param-value>
      </context-param>
      <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>servicelayer-context</param-value>
      </context-param>

    and in beanRefContext.xml, somewhere on your classpath:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- $Id&#58; beanRefContext.xml,v 1.1 2004/11/14 17&#58;19&#58;19 colins Exp $ -->
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <!-- load a hierarchy of contexts, although there is just one here -->
    
    <beans>
    
      <bean id="servicelayer-context"
      	    class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
          <list>
            <value>/WEB-INF/applicationContext2.xml</value>
          </list>
       	</constructor-arg>
      </bean>
    
    </beans>

    The loading of the shared parent context happens via ContextSingletonBeanFactoryLocator. Take a look at the JavaDocs for that to get an idea of what is going on.

    Note this is a _shared_ parent context. Any other webapps in the same EAR file (if uisng an EAR file) that specify the same beanRefContext will share the same parent context, and the same woiuld happen with any other utility code that did the same thing...

    Regards,
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  3. #3
    Join Date
    Aug 2004
    Posts
    9

    Default

    Why isn't this topic covered in the Spring documentation?

    Thanks Colin for the post.

    Tag: ContextLoader, Sharing Beans Between Web Applications, Loading Beans Once.

Similar Threads

  1. Replies: 4
    Last Post: Sep 27th, 2005, 11:31 PM
  2. How do you create unit tests for layered applications?
    By paul.barry in forum Architecture
    Replies: 2
    Last Post: May 6th, 2005, 08:31 AM
  3. How to create hierarchical context
    By Lock51 in forum Container
    Replies: 4
    Last Post: Mar 24th, 2005, 02:30 PM
  4. Replies: 10
    Last Post: Nov 2nd, 2004, 09:38 AM
  5. Should Contexts themselves be configured by DI?
    By jbetancourt in forum Architecture
    Replies: 5
    Last Post: Sep 1st, 2004, 01:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •