private FlashMapManager getFlashMapManager(MockHttpServletRequest request) { FlashMapManager flashMapManager = null; try { ServletContext servletContext = request.getServletContext(); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); flashMapManager = wac.getBean(DispatcherServlet.FLASH_MAP_MANAGER_BEAN_NAME, FlashMapManager.class); } catch (IllegalStateException | NoSuchBeanDefinitionException ex) { // ignore } return (flashMapManager != null ? flashMapManager : new SessionFlashMapManager()); }
@Override public void init() throws ServletException { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); this.target = wac.getBean(getServletName(), HttpRequestHandler.class); }
/** * Look up the SessionFactory that this filter should use. * <p>The default implementation looks for a bean with the specified name * in Spring's root application context. * @return the SessionFactory to use * @see #getSessionFactoryBeanName */ protected SessionFactory lookupSessionFactory() { if (logger.isDebugEnabled()) { logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter"); } WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class); }
@Override public void init() throws ServletException { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); this.target = wac.getBean(getServletName(), HttpRequestHandler.class); }
/** * Look up the EntityManagerFactory that this filter should use. * <p>The default implementation looks for a bean with the specified name * in Spring's root application context. * @return the EntityManagerFactory to use * @see #getEntityManagerFactoryBeanName */ protected EntityManagerFactory lookupEntityManagerFactory() { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); String emfBeanName = getEntityManagerFactoryBeanName(); String puName = getPersistenceUnitName(); if (StringUtils.hasLength(emfBeanName)) { return wac.getBean(emfBeanName, EntityManagerFactory.class); } else if (!StringUtils.hasLength(puName) && wac.containsBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME)) { return wac.getBean(DEFAULT_ENTITY_MANAGER_FACTORY_BEAN_NAME, EntityManagerFactory.class); } else { // Includes fallback search for single EntityManagerFactory bean by type. return EntityManagerFactoryUtils.findEntityManagerFactory(wac, puName); } }
/** * Process {@code @Autowired} injection for the given target object, * based on the current root web application context as stored in the ServletContext. * <p>Intended for use as a delegate. * @param target the target object to process * @param servletContext the ServletContext to find the Spring web application context in * @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext) */ public static void processInjectionBasedOnServletContext(Object target, ServletContext servletContext) { Assert.notNull(target, "Target object must not be null"); WebApplicationContext cc = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(cc.getAutowireCapableBeanFactory()); bpp.processInjection(target); }
/** * Process {@code @Autowired} injection for the given target object, * based on the current root web application context as stored in the ServletContext. * <p>Intended for use as a delegate. * @param target the target object to process * @param servletContext the ServletContext to find the Spring web application context in * @see WebApplicationContextUtils#getWebApplicationContext(javax.servlet.ServletContext) */ public static void processInjectionBasedOnServletContext(Object target, ServletContext servletContext) { Assert.notNull(target, "Target object must not be null"); WebApplicationContext cc = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(cc.getAutowireCapableBeanFactory()); bpp.processInjection(target); }
@Test public void testContextLoaderListenerWithLocalContextInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString( new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()})); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithMixedContextInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName()); sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithGlobalContextInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString( new Object[] {TestContextInitializer.class.getName(), TestWebContextInitializer.class.getName()})); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithProgrammaticAndLocalInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.setContextInitializers(new TestWebContextInitializer()); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithProgrammaticAndGlobalInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.setContextInitializers(new TestContextInitializer()); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void testContextLoaderListenerWithProgrammaticInitializers() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml"); ContextLoaderListener listener = new ContextLoaderListener(); listener.setContextInitializers(new TestContextInitializer(), new TestWebContextInitializer()); listener.contextInitialized(new ServletContextEvent(sc)); WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc); TestBean testBean = wac.getBean(TestBean.class); assertThat(testBean.getName(), equalTo("testName")); assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue()); }
@Test public void register() throws ServletException { initializer.onStartup(servletContext); assertTrue(eventListener instanceof ContextLoaderListener); ContextLoaderListener cll = (ContextLoaderListener) eventListener; cll.contextInitialized(new ServletContextEvent(servletContext)); WebApplicationContext applicationContext = WebApplicationContextUtils .getRequiredWebApplicationContext(servletContext); assertTrue(applicationContext.containsBean(BEAN_NAME)); assertTrue(applicationContext.getBean(BEAN_NAME) instanceof MyBean); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributeNotPreviouslySet() { StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext); DefaultMockMvcBuilder builder = webAppContextSetup(root); WebApplicationContext wac = builder.initWebAppContext(); assertSame(root, wac); assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
/** * See SPR-12553 and SPR-13075. */ @Test public void rootWacServletContainerAttributePreviouslySet() { StubWebApplicationContext child = new StubWebApplicationContext(this.servletContext); this.servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, child); DefaultMockMvcBuilder builder = webAppContextSetup(child); assertSame(builder.initWebAppContext(), WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext)); }
@Test // SPR-12553 public void applicationContextAttribute() { TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController()); builder.addPlaceholderValue("sys.login.ajax", "/foo"); WebApplicationContext wac = builder.initWebAppContext(); assertEquals(wac, WebApplicationContextUtils.getRequiredWebApplicationContext(wac.getServletContext())); }
/** * 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 testContextLoaderListenerWithDefaultContext() { MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml"); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); listener.contextInitialized(event); String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr); assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext); assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext); LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle"); assertTrue("Has father", context.containsBean("father")); assertTrue("Has rod", context.containsBean("rod")); assertTrue("Has kerry", context.containsBean("kerry")); assertTrue("Not destroyed", !lb.isDestroyed()); assertFalse(context.containsBean("beans1.bean1")); assertFalse(context.containsBean("beans1.bean2")); listener.contextDestroyed(event); assertTrue("Destroyed", lb.isDestroyed()); assertNull(sc.getAttribute(contextAttr)); assertNull(WebApplicationContextUtils.getWebApplicationContext(sc)); }
/** * 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)); }