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;
}
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);
}
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);
}
}
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());
}
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);
}
}
Aggregations