Search in sources :

Example 86 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.

the class FrameworkServlet method findWebApplicationContext.

/**
	 * Retrieve a {@code WebApplicationContext} from the {@code ServletContext}
	 * attribute with the {@link #setContextAttribute configured name}. The
	 * {@code WebApplicationContext} must have already been loaded and stored in the
	 * {@code ServletContext} before this servlet gets initialized (or invoked).
	 * <p>Subclasses may override this method to provide a different
	 * {@code WebApplicationContext} retrieval strategy.
	 * @return the WebApplicationContext for this servlet, or {@code null} if not found
	 * @see #getContextAttribute()
	 */
protected WebApplicationContext findWebApplicationContext() {
    String attrName = getContextAttribute();
    if (attrName == null) {
        return null;
    }
    WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);
    if (wac == null) {
        throw new IllegalStateException("No WebApplicationContext found: initializer not registered?");
    }
    return wac;
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext)

Example 87 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.

the class AnnotationConfigDispatcherServletInitializerTests method rootContextOnly.

// SPR-11357
@Test
public void rootContextOnly() throws ServletException {
    initializer = new MyAnnotationConfigDispatcherServletInitializer() {

        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class<?>[] { MyConfiguration.class };
        }

        @Override
        protected Class<?>[] getServletConfigClasses() {
            return null;
        }
    };
    initializer.onStartup(servletContext);
    DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
    servlet.init(new MockServletConfig(this.servletContext));
    WebApplicationContext wac = servlet.getWebApplicationContext();
    ((AnnotationConfigWebApplicationContext) wac).refresh();
    assertTrue(wac.containsBean("bean"));
    assertTrue(wac.getBean("bean") instanceof MyBean);
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 88 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.

the class WebApplicationContextScopeTests method testSessionScope.

@Test
public void testSessionScope() {
    WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_SESSION);
    MockHttpServletRequest request = new MockHttpServletRequest();
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    try {
        assertNull(request.getSession().getAttribute(NAME));
        DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
        assertSame(bean, request.getSession().getAttribute(NAME));
        assertSame(bean, ac.getBean(NAME));
        request.getSession().invalidate();
        assertTrue(bean.wasDestroyed());
    } finally {
        RequestContextHolder.setRequestAttributes(null);
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 89 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.

the class WebApplicationContextScopeTests method testApplicationScope.

@Test
public void testApplicationScope() {
    WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION);
    assertNull(ac.getServletContext().getAttribute(NAME));
    DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
    assertSame(bean, ac.getServletContext().getAttribute(NAME));
    assertSame(bean, ac.getBean(NAME));
    new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext()));
    assertTrue(bean.wasDestroyed());
}
Also used : DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ContextCleanupListener(org.springframework.web.context.ContextCleanupListener) ServletContextEvent(javax.servlet.ServletContextEvent) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 90 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.

the class MvcUriComponentsBuilder method getRequestMappingInfoHandlerMapping.

private static RequestMappingInfoHandlerMapping getRequestMappingInfoHandlerMapping() {
    WebApplicationContext wac = getWebApplicationContext();
    Assert.notNull(wac, "Cannot lookup handler method mappings without WebApplicationContext");
    try {
        return wac.getBean(RequestMappingInfoHandlerMapping.class);
    } catch (NoUniqueBeanDefinitionException ex) {
        throw new IllegalStateException("More than one RequestMappingInfoHandlerMapping beans found", ex);
    } catch (NoSuchBeanDefinitionException ex) {
        throw new IllegalStateException("No RequestMappingInfoHandlerMapping bean", ex);
    }
}
Also used : NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(org.springframework.beans.factory.NoUniqueBeanDefinitionException) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Aggregations

WebApplicationContext (org.springframework.web.context.WebApplicationContext)103 Test (org.junit.Test)32 ServletContext (javax.servlet.ServletContext)17 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)15 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)13 ApplicationContext (org.springframework.context.ApplicationContext)11 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)11 HashMap (java.util.HashMap)9 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)9 MockServletContext (org.springframework.mock.web.test.MockServletContext)8 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)7 Filter (javax.servlet.Filter)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)6 Binding (groovy.lang.Binding)5 BeanBuilder (hudson.util.spring.BeanBuilder)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)5 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)4 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)4