Announcement Announcement Module
Collapse
No announcement yet.
Single shared applicationContext or Hierarchy? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Single shared applicationContext or Hierarchy?

    Hi,

    I know this subject has been discussed many times, but as I read through all threads, I got confused. Please help me clarify an applicationContext issue.



    I have an EAR application that has a WAR and JAR (MDBs). I’d like to have a single application context for both WAR and JAR. Since a WAR needs web application context, my guess is that a JAR must access that context. And here is my question.

    Q1. Is it possible to have a single context (probably, webApplicationContext) for WAR and JAR?

    If so, how to achieve that? I could have different XML files for each but want to load them onto a single context.



    If it’s impossible, another solution would be to have hierarchy contexts. I could have two contexts (one for a WAR and the other for a JAR), then one context will be a parent of the other. This is where I got confused.

    Q2. What are benefits of creating tree hierarchy context structure?

    Can a child context inherit a parent’s beans and access them? Can Spring container-based declarative IoC still work between parent and child context beans (e.g. refer to beans in a parent context by using <ref bean=””> in child applicationContext.xml)? Or do I have to programmatically get beans in a parent context (e.g. using ContextSingletonBeanFactoryLocator…) and inject them into beans in a child context?



    Thank you for your support in advance.

  • #2
    Re: Single shared applicationContext or Hierarchy?

    Check discussion:

    http://forum.springframework.org/showthread.php?t=14607

    where it points out a way to share the applicationContext (with an example tied to MDB).

    It should answer your questions.

    Dino
    Last edited by Colin Sampaleanu; Nov 26th, 2005, 09:59 AM.

    Comment


    • #3
      Also see this thread:
      http://forum.springframework.org/showthread.php?t=10390

      Essentially, ContextSingletonBeanFactoryLocator may be used to access one shared context, from your own 'glue' code. As for the web app itself, which comes in as a WAR file, ContextLoader is automatically able to also trigger the loading (or reuse) of a shared parent context to the main web-app context. This shared context can be the one and the same, all over the place.

      Regards,
      Last edited by Colin Sampaleanu; Nov 26th, 2005, 10:02 AM.

      Comment


      • #4
        Thank you for your reply. Yes, I had read both threads. I eventually created two contexts (one for web and the other for jar) and jar context is a parent of web app context now.

        I didn't know <ref> has "parent" attribute, but everything now worked out.

        After I checked the server log, however, I found out that jar context was created twice along with all beans as web app started and MDB was processed. My app runs on single machine with single JVM, and I'd like to avoid this unnecessary resource allocation.

        How can MDB get a root context that has been already created? Here is my MDB code. I have beanRefContext.xml in root classpath (default), so I didn't pass any args to a getInstance method. This works fine except that it creates a new context. Yes, it creates only once (all MDBs share it), but the same context was already created as a web app started.

        Code:
        BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance&#40;&#41;;
        BeanFactoryReference ref = locator.useBeanFactory&#40;"myContextId"&#41;;
        I'm using JBoss 4.x. Is this a class loader issue?

        Again, thank you for your support.

        Comment


        • #5
          Did you try the approach that I pointed to you? (Look at the last message and try the exact approach to see if it works).

          Specifically:

          ContextSingletonBeanFactoryLocator.getInstance("cl asspath*:spring/beanRefContext.xml")

          in the lookup.

          I'm assuming that the original author got this to work properly.


          Dino

          Comment

          Working...
          X