Announcement Announcement Module
Collapse
No announcement yet.
sharing context with embedded webapp Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • sharing context with embedded webapp

    I've created a simple middleware platform and wish to expose some of its functionality as a web application. The existing platform already has an ApplicationContext built at startup with the non-webapp components necessary for the webapp to work.

    What I'd like to do is fire up the embedded web server (Jetty) after the main ApplicationContext is built. Then we'd have a simple webapp context declaration with the webapp-only components (url mappings, controllers, etc.) and then reference the already-created context when building the webapp context so the controllers can use components from the existing ApplicationContext.

    In http://forum.springframework.org/showthread.php?t=16005 someone brings up a similar problem but the answer (referencing the same xml files) seems to be to create a separate context, which doesn't sound right to me.

    My question is this: is it possible to point the DispatcherServlet to an existing context as a parent? Or would it be smarter to simply subclass DispatcherServlet and override createWebApplicationContext() from FrameworkServlet?
    Last edited by robyn; May 14th, 2006, 08:45 PM.

  • #2
    solution: use existing as parent

    Maybe I should have actually tried subclassing before I posted because it was not only easy, it worked. Here's the subclass:

    Code:
    public class SpringDispatcherServlet
            extends DispatcherServlet
    {
        private static Logger log = Logger.getLogger( SpringDispatcherServlet.class );
    
        @Override
        protected WebApplicationContext
                createWebApplicationContext( WebApplicationContext parent )
                throws BeansException
        {
            String prefix = "Servlet [" + getServletName() + "] ";
            log.info( prefix + "Creating webapp context" );
            ConfigurableWebApplicationContext wac = new XmlWebApplicationContext();
            wac.setServletContext( getServletContext() );
            wac.setNamespace( getNamespace() );
    
            // Use our main app context as the parent
            ApplicationContext ctx = Util.getApplicationContext();
            wac.setParent( ctx );
    
            // ...then assign other context(s) for this webapp
            String webappCtx = getContextConfigLocation();
            if ( Util.isBlank( webappCtx ) ) {
                throw new ApplicationContextException(
                        "Application servlet " + getServletName() + " must define a " +
                        "servlet parameter 'contextConfigLocation' with one or more " +
                        "comma-delimited classpath references to webapp context(s)." );
            }
            log.info( prefix + "Assigning additional config contexts: " + webappCtx );
            wac.setConfigLocations( webappCtx.split( "\\s*,\\s*" ) );
    
            // this does additional initialization: do not remove
            wac.refresh();
            log.info( prefix + "Webapp context created/refreshed ok" );
            return wac;
        }
    }

    Comment

    Working...
    X