Announcement Announcement Module
No announcement yet.
sharing context with embedded webapp Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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 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, 07: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:

    public class SpringDispatcherServlet
            extends DispatcherServlet
        private static Logger log = Logger.getLogger( SpringDispatcherServlet.class );
        protected WebApplicationContext
                createWebApplicationContext( WebApplicationContext parent )
                throws BeansException
            String prefix = "Servlet [" + getServletName() + "] ";
   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)." );
   prefix + "Assigning additional config contexts: " + webappCtx );
            wac.setConfigLocations( webappCtx.split( "\\s*,\\s*" ) );
            // this does additional initialization: do not remove
   prefix + "Webapp context created/refreshed ok" );
            return wac;