Package org.springframework.web.context

Examples of org.springframework.web.context.ConfigurableWebApplicationContext


          }
          configLocation.append(part);
        }
      }
    }
    ConfigurableWebApplicationContext applicationContext = new XmlWebApplicationContext();

    // Assign the best possible id value.
    String id;
    try {
      String contextPath = (String) ServletContext.class.getMethod("getContextPath").invoke(servletContext);
      id = ObjectUtils.getDisplayString(contextPath);
    } catch (Exception ex) {
      // Servlet <= 2.4: resort to name specified in web.xml, if any.
      String servletContextName = servletContext.getServletContextName();
      id = ObjectUtils.getDisplayString(servletContextName);     
    }

    applicationContext.setId("Geomajas:" + id);
    applicationContext.setServletContext(servletContext);
    applicationContext.setConfigLocation(configLocation.toString());
    applicationContext.refresh();

    ApplicationContextUtil.setApplicationContext(servletContext, applicationContext);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
  }
View Full Code Here


{
    @Test
    public void load_application_context_externally()
    {
        ServletContext servletContext = mockServletContext();
        ConfigurableWebApplicationContext ac = newMock(ConfigurableWebApplicationContext.class);
        Runnable fred = mockRunnable();
        Runnable barney = mockRunnable();

        ServiceBuilderResources resources = mockServiceBuilderResources();

        train_getInitParameter(servletContext, SpringConstants.USE_EXTERNAL_SPRING_CONTEXT, "true");

        train_getAttribute(servletContext, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
        expect(ac.getBeanDefinitionNames()).andReturn(new String[] {"fred", "&barney"});

        replay();

        SpringModuleDef moduleDef = new SpringModuleDef(servletContext);

        ServiceDef serviceDef = moduleDef.getServiceDef(SpringModuleDef.SERVICE_ID);

        ObjectCreator serviceCreator = serviceDef.createServiceCreator(resources);

        assertSame(serviceCreator.createObject(), ac);

        verify();

        // Now, let's test for some of the services.

        ServiceDef sd = moduleDef.getServiceDef("ApplicationContext");

        assertEquals(sd.getServiceInterface(), ac.getClass());
        assertEquals(sd.createServiceCreator(null).toString(),
                     "<ObjectCreator for externally configured Spring ApplicationContext>");

        expect((Class)ac.getType("fred")).andReturn(Runnable.class);
        expect(ac.getBean("fred")).andReturn(fred);


        sd = moduleDef.getServiceDef("fred");

        replay();

        assertEquals(sd.getServiceId(), "fred");
        assertEquals(sd.getServiceInterface(), Runnable.class);
        assertEquals(sd.getServiceScope(), ScopeConstants.DEFAULT);
        assertSame(sd.createServiceCreator(null).createObject(), fred);
        assertTrue(sd.getMarkers().isEmpty());
        assertFalse(sd.isEagerLoad());
        assertEquals(sd.createServiceCreator(null).toString(), "ObjectCreator<Spring Bean 'fred'>");

        verify();

        expect((Class)ac.getType("barney")).andReturn(Runnable.class);
        expect(ac.getBean("barney")).andReturn(barney);

        replay();

        sd = moduleDef.getServiceDef("barney");
View Full Code Here

    public void testWithSpringContext() throws Exception {
        Container container = EasyMock.createNiceMock(Container.class);
        EasyMock.replay(container);

        ConfigurableWebApplicationContext ac = new XmlWebApplicationContext();
        ServletContext msc = (ServletContext) new MockServletContext();
        msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
        ac.setServletContext(msc);
        ac.setConfigLocations(new String[] {"org/apache/struts2/spring/StrutsSpringObjectFactoryTest-applicationContext.xml"});
        ac.refresh();
        StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory("constructor", null, null, msc, null, container);

        assertEquals(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, fac.getAutowireStrategy());
    }
View Full Code Here

   * @throws BeansException
   */
  protected final ConfigurableWebApplicationContext createWebApplicationContext(
    final WebApplicationContext parent, final WicketFilter filter) throws BeansException
  {
    ConfigurableWebApplicationContext wac = newApplicationContext();
    wac.setParent(parent);
    wac.setServletContext(filter.getFilterConfig().getServletContext());
    wac.setConfigLocation(getContextConfigLocation(filter));

    postProcessWebApplicationContext(wac, filter);
    wac.refresh();

    return wac;
  }
View Full Code Here

          "Fatal initialization error in servlet with name '" + getServletName() +
          "': custom WebApplicationContext class [" + getContextClass().getName() +
          "] is not of type ConfigurableWebApplicationContext");
    }

    ConfigurableWebApplicationContext wac =
        (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass());
    wac.setParent(parent);
    wac.setServletContext(getServletContext());
    wac.setServletConfig(getServletConfig());
    wac.setNamespace(getNamespace());
    wac.setConfigLocation(getContextConfigLocation());
    wac.addApplicationListener(new SourceFilteringListener(wac, this));

    postProcessWebApplicationContext(wac);
    wac.refresh();

    return wac;
  }
View Full Code Here

    }

    public void testWithSpringContext() throws Exception {
       

        ConfigurableWebApplicationContext ac = new XmlWebApplicationContext();
        ServletContext msc = (ServletContext) new MockServletContext();
        msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
        ac.setServletContext(msc);
        ac.setConfigLocations(new String[] {"org/apache/struts2/spring/StrutsSpringObjectFactoryTest-applicationContext.xml"});
        ac.refresh();
        StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory("constructor", null, msc);

        assertEquals(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, fac.getAutowireStrategy());
    }
View Full Code Here

        StrutsSpringObjectFactory fac = new StrutsSpringObjectFactory();

        // autowire by constructure, we try a non default setting in this unit test
        Settings.set(StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE, "constructor");

        ConfigurableWebApplicationContext ac = new XmlWebApplicationContext();
        ServletContext msc = (ServletContext) new MockServletContext();
        msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ac);
        ac.setServletContext(msc);
        ac.setConfigLocations(new String[] {"org/apache/struts2/spring/StrutsSpringObjectFactoryTest-applicationContext.xml"});
        ac.refresh();

        fac.init(msc);

        assertEquals(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, fac.getAutowireStrategy());
    }
View Full Code Here

          newContext = super.createWebApplicationContext(servletContext);
         
          //use the configure method to do configuration on the web application context
          if (newContext instanceof ConfigurableWebApplicationContext) {
           
            ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) newContext;
          if (configurableMethod != null) {
           
            ReflectionUtils.invokeMethod(configurableMethod, this, new Object[]{
              parent,
              servletContext
            });
          }
         
          wac.setParent(parent);
          wac.setServletContext(servletContext);
          }
       
        } else {
         
          //for 3.0 and below
View Full Code Here

    if (this.webApplicationContext != null) {
      // A context instance was injected at construction time -> use it
      wac = this.webApplicationContext;
      if (wac instanceof ConfigurableWebApplicationContext) {
        ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
        if (!cwac.isActive()) {
          // The context has not yet been refreshed -> provide services such as
          // setting the parent context, setting the application context id, etc
          if (cwac.getParent() == null) {
            // The context instance was injected without an explicit parent -> set
            // the root application context (if any; may be null) as the parent
            cwac.setParent(rootContext);
          }
          configureAndRefreshWebApplicationContext(cwac);
        }
      }
    }
View Full Code Here

      throw new ApplicationContextException(
          "Fatal initialization error in servlet with name '" + getServletName() +
          "': custom WebApplicationContext class [" + contextClass.getName() +
          "] is not of type ConfigurableWebApplicationContext");
    }
    ConfigurableWebApplicationContext wac =
        (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

    wac.setEnvironment(getEnvironment());
    wac.setParent(parent);
    wac.setConfigLocation(getContextConfigLocation());

    configureAndRefreshWebApplicationContext(wac);

    return wac;
  }
View Full Code Here

TOP

Related Classes of org.springframework.web.context.ConfigurableWebApplicationContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.