Announcement Announcement Module
No announcement yet.
Hierarchical application context in J2EE app Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hierarchical application context in J2EE app

    I have a J2EE application (ear) with a web module, several jar modules, and a sar module (service archive - maybe specific to JBoss?).

    The sar module starts up a JBoss scheduled task, and has its own Spring xml config file to create its own application context.

    Currently this is quite separate from the web application context, which is created when the DispatcherServlet starts up from its own Spring xml config files.

    This works OK, but there is some duplication between the two application contexts, and in particular I have to duplicate some properties files for the two contexts, which is clearly a Bad Thing.

    I would like to configure it so the web context is a child of the sar context (the sar module starts up first - I'm not aware that that is configurable).

    How can I do that? How can I make the context created by the sar module available to the DispatcherServlet? (the only idea I had was to register it with JNDI, but I read in a thread that that's illegal).
    And supposing I can get at it from the DispatcherServlet, how do I configure that to use it as a parent context?


  • #2
    I have a similar challenge. I have to get it to work on both weblogic and websphere (and I still have hair on my head).

    Anyway, I'm trying to use ContextSingletonBeanFactoryLocator and follow Colin's ejbtest 'example' and other resources.


    • #3
      I've got something that seems to do what I want. I'll post the relevant bits of code here in the hope that
      a) If it's not good practice someone can tell me; and
      b) If it's OK, it may be a useful example.

      The constructor of the schdulable object in my sar:
          public OrderUnload()
              // do it this way to make the beanFactory available to web modules:
              BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
              BeanFactoryReference bfr = bfl.useBeanFactory("org.stl.wo.appcontext");
              //to prevent classloader problems on hot redeploy:
      The DespatcherServlet for my web module:
      public class CustomContextDispatcherServlet extends DispatcherServlet {
          BeanFactoryReference bfr = null;
          protected WebApplicationContext initWebApplicationContext() {
              ContextLoader cl = new MyContextLoader();
              WebApplicationContext wac = cl.initWebApplicationContext(this.getServletContext());
              getServletContext().setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.woapp", wac);
              return wac;
          public void destroy() {
              // to prevent classloader problems on hot redeploy:
              if (bfr != null) {
          class MyContextLoader extends ContextLoader {
              protected ApplicationContext loadParentContext(ServletContext servletContext) {
                  BeanFactoryLocator bfl = SingletonBeanFactoryLocator.getInstance();
                  bfr = bfl.useBeanFactory("org.stl.wo.appcontext");
                  return (ApplicationContext)bfr.getFactory();
      I found the calls to release() on the BeanFactoryReference to be necessary to prevent classloader problems on hot redeploy of the application.