@Override protected WebApplicationContext initWebAppContext() { ServletContext servletContext = this.webAppContext.getServletContext(); Assert.state(servletContext != null, "No ServletContext"); ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext); if (rootWac == null) { rootWac = this.webAppContext; ApplicationContext parent = this.webAppContext.getParent(); while (parent != null) { if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) { rootWac = parent; break; } parent = parent.getParent(); } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); } return this.webAppContext; }
@Test public void verifyRootWacConfig() { ApplicationContext parent = wac.getParent(); assertNotNull(parent); assertFalse(parent instanceof WebApplicationContext); assertEquals("ear", ear); assertEquals("root", root); }
@Test public void verifyRootWacSupport() { assertEquals("foo", foo); assertEquals("bar", bar); ApplicationContext parent = wac.getParent(); assertNotNull(parent); assertTrue(parent instanceof WebApplicationContext); WebApplicationContext root = (WebApplicationContext) parent; assertFalse(root.getBeansOfType(String.class).containsKey("bar")); ServletContext childServletContext = wac.getServletContext(); assertNotNull(childServletContext); ServletContext rootServletContext = root.getServletContext(); assertNotNull(rootServletContext); assertSame(childServletContext, rootServletContext); assertSame(root, rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); assertSame(root, childServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); }
/** * Verify that the breaking change introduced in <a * href="https://jira.spring.io/browse/SPR-12553">SPR-12553</a> has been reverted. * * <p>This code has been copied from * {@link org.springframework.test.context.hierarchies.web.ControllerIntegrationTests}. * * @see org.springframework.test.context.hierarchies.web.ControllerIntegrationTests#verifyRootWacSupport() */ private void verifyRootWacSupport() { assertNotNull(personDao); assertNotNull(personController); ApplicationContext parent = wac.getParent(); assertNotNull(parent); assertTrue(parent instanceof WebApplicationContext); WebApplicationContext root = (WebApplicationContext) parent; ServletContext childServletContext = wac.getServletContext(); assertNotNull(childServletContext); ServletContext rootServletContext = root.getServletContext(); assertNotNull(rootServletContext); assertSame(childServletContext, rootServletContext); assertSame(root, rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); assertSame(root, childServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributeNotPreviouslySetWithContextHierarchy() { StaticApplicationContext ear = new StaticApplicationContext(); StaticWebApplicationContext root = new StaticWebApplicationContext(); root.setParent(ear); root.setServletContext(this.servletContext); StaticWebApplicationContext dispatcher = new StaticWebApplicationContext(); dispatcher.setParent(root); dispatcher.setServletContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(dispatcher); WebApplicationContext wac = builder.initWebAppContext(); assertSame(dispatcher, wac); assertSame(root, wac.getParent()); assertSame(ear, wac.getParent().getParent()); assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributePreviouslySetWithContextHierarchy() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, root); StaticWebApplicationContext child = new StaticWebApplicationContext(); child.setParent(root); child.setServletContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(child); assertSame(builder.initWebAppContext().getParent(), WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
@Test public void verifyDispatcherWacConfig() { ApplicationContext parent = wac.getParent(); assertNotNull(parent); assertTrue(parent instanceof WebApplicationContext); ApplicationContext grandParent = parent.getParent(); assertNotNull(grandParent); assertFalse(grandParent instanceof WebApplicationContext); ServletContext dispatcherServletContext = wac.getServletContext(); assertNotNull(dispatcherServletContext); ServletContext rootServletContext = ((WebApplicationContext) parent).getServletContext(); assertNotNull(rootServletContext); assertSame(dispatcherServletContext, rootServletContext); assertSame(parent, rootServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); assertSame(parent, dispatcherServletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)); assertEquals("ear", ear); assertEquals("root", root); assertEquals("dispatcher", dispatcher); }
public static void configureServletContextAttributes(ServletContext servletContext, GrailsApplication application, GrailsPluginManager pluginManager, WebApplicationContext webContext) { servletContext.setAttribute(ApplicationAttributes.PLUGIN_MANAGER, pluginManager); // use config file locations if available servletContext.setAttribute(ApplicationAttributes.PARENT_APPLICATION_CONTEXT, webContext.getParent()); servletContext.setAttribute(GrailsApplication.APPLICATION_ID, application); servletContext.setAttribute(ApplicationAttributes.APPLICATION_CONTEXT, webContext); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webContext); }
@Override protected WebApplicationContext initWebAppContext() { ServletContext servletContext = this.webAppContext.getServletContext(); Assert.state(servletContext != null, "No ServletContext"); ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext); if (rootWac == null) { rootWac = this.webAppContext; ApplicationContext parent = this.webAppContext.getParent(); while (parent != null) { if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) { rootWac = parent; break; } parent = parent.getParent(); } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); } return this.webAppContext; }
@Override protected WebApplicationContext initWebAppContext() { ServletContext servletContext = this.webAppContext.getServletContext(); ApplicationContext rootWac = WebApplicationContextUtils.getWebApplicationContext(servletContext); if (rootWac == null) { rootWac = this.webAppContext; ApplicationContext parent = this.webAppContext.getParent(); while (parent != null) { if (parent instanceof WebApplicationContext && !(parent.getParent() instanceof WebApplicationContext)) { rootWac = parent; break; } parent = parent.getParent(); } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, rootWac); } return this.webAppContext; }
public abstract class AutowireCapableDispatcherServlet extends DispatcherServlet { @Override protected void initFrameworkServlet() { super.initFrameworkServlet(); AutowireCapableBeanFactory autowireCapableBeanFactory = wac .getAutowireCapableBeanFactory(); autowireCapableBeanFactory.autowireBean(this); WebApplicationContext wac = getWebApplicationContext(); ApplicationContext parent = wac.getParent(); if(parent != null){ autowireCapableBeanFactory = parent.getAutowireCapableBeanFactory(); autowireCapableBeanFactory.autowireBean(this); } } }
private void debug(final WebApplicationContext ctx) { // DEBUG if ( DEBUG ) { logger.debug("SearchServlet: Context Display name = " + ctx.getDisplayName()); logger.debug("SearchServlet: Context ID = " + ctx.getId()); if (ctx.getParent() == null) { logger.debug("SearchServlet: Context Parent is null."); } else { logger.debug("SearchServlet: Context Parent Display name = " + ctx.getParent().getDisplayName()); } if (ctx.getServletContext() == null) { logger.debug("SearchServlet: ServletContext is null."); } else { logger.debug("SearchServlet: ServletContext path = " + ctx.getServletContext().getContextPath()); logger.debug("SearchServlet: ServletContext context name = " + ctx.getServletContext().getServletContextName()); } } // DEBUG }
logger.debug( "InstanceServlet: Context Display name = " + ctx.getDisplayName() ); logger.debug( "InstanceServlet: Context ID = " + ctx.getId() ); if ( ctx.getParent() == null ) { logger.debug( "InstanceServlet: Context Parent is null." ); } else { logger.debug( "InstanceServlet: Context Parent Display name = " + ctx.getParent().getDisplayName() );