super.contextInitialized(sce);
@Override public void contextInitialized(ServletContextEvent event) { super.contextInitialized(event); ApplicationContext appContext = getCurrentWebApplicationContext(); if (appContext != null) { appContext.publishEvent(new ContextLoadedEvent(appContext)); } } }
@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 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 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()); }
/** * If context config locations have been specified neither against the application * context nor the context loader listener, then fall back to default values. */ @Test public void abstractRefreshableWAC_fallsBackToConventionBasedNaming() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); // no init-param set //sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML System.out.println(t.getMessage()); assertTrue(t.getMessage().endsWith( "Could not open ServletContext resource [/WEB-INF/applicationContext.xml]")); } }
@Test public void abstractRefreshableWAC_respectsProgrammaticConfigLocations() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("programmatic.xml"); ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage(), t.getMessage().endsWith( "Could not open ServletContext resource [/programmatic.xml]")); } }
/** * Ensure that any custom default locations are still respected. */ @Test public void customAbstractRefreshableWAC_fallsBackToInitParam() { XmlWebApplicationContext ctx = new XmlWebApplicationContext() { @Override protected String[] getDefaultConfigLocations() { return new String[] { "/WEB-INF/custom.xml" }; } }; //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML System.out.println(t.getMessage()); assertTrue(t.getMessage().endsWith( "Could not open ServletContext resource [/from-init-param.xml]")); } }
@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 testRegisteredContextInitializerCanAccessServletContextParamsViaEnvironment() { MockServletContext sc = new MockServletContext(""); // config file doesn't matter - just a placeholder sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/empty-context.xml"); sc.addInitParameter("someProperty", "someValue"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, EnvApplicationContextInitializer.class.getName()); ContextLoaderListener listener = new ContextLoaderListener(); listener.contextInitialized(new ServletContextEvent(sc)); }
@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); }
/** * If a contextConfigLocation init-param has been specified for the ContextLoaderListener, * then it should take precedence. This is generally not a recommended practice, but * when it does happen, the init-param should be considered more specific than the * programmatic configuration, given that it still quite possibly externalized in * hybrid web.xml + WebApplicationInitializer cases. */ @Test public void abstractRefreshableWAC_respectsInitParam_overProgrammaticConfigLocations() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("programmatic.xml"); ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage(), t.getMessage().endsWith( "Could not open ServletContext resource [/from-init-param.xml]")); } }
/** * If setConfigLocation has not been called explicitly against the application context, * then fall back to the ContextLoaderListener init-param if present. */ @Test public void abstractRefreshableWAC_fallsBackToInitParam() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage().endsWith( "Could not open ServletContext resource [/from-init-param.xml]")); } }
@Test public void testContextLoaderListenerWithUnknownContextInitializer() { MockServletContext sc = new MockServletContext(""); // config file doesn't matter. just a placeholder sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/empty-context.xml"); sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, StringUtils.arrayToCommaDelimitedString(new Object[] {UnknownContextInitializer.class.getName()})); ContextLoaderListener listener = new ContextLoaderListener(); try { listener.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (ApplicationContextException ex) { assertTrue(ex.getMessage().contains("not assignable")); } }
/** * Ensure that ContextLoaderListener and AnnotationConfigApplicationContext interact nicely. */ @Test public void annotationConfigWAC() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.scan("does.not.matter"); ContextLoaderListener cll = new ContextLoaderListener(ctx); cll.contextInitialized(new ServletContextEvent(new MockServletContext())); } }
/** * Ensure that ContextLoaderListener and GenericWebApplicationContext interact nicely. */ @Test public void genericWAC() { GenericWebApplicationContext ctx = new GenericWebApplicationContext(); ContextLoaderListener cll = new ContextLoaderListener(ctx); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(ctx); scanner.scan("bogus.pkg"); cll.contextInitialized(new ServletContextEvent(new MockServletContext())); }
@Override public void contextInitialized(ServletContextEvent event) { try { cloudStackContext = new CloudStackSpringContext(); cloudStackContext.registerShutdownHook(); event.getServletContext().setAttribute(CloudStackSpringContext.CLOUDSTACK_CONTEXT_SERVLET_KEY, cloudStackContext); } catch (IOException e) { log.error("Failed to start CloudStack", e); throw new RuntimeException("Failed to initialize CloudStack Spring modules", e); } configuredParentName = event.getServletContext().getInitParameter(WEB_PARENT_MODULE); if (configuredParentName == null) { configuredParentName = WEB_PARENT_MODULE_DEFAULT; } super.contextInitialized(event); }
@Override public void contextInitialized(ServletContextEvent theSce) { theSce.getServletContext().setInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, AnnotationConfigWebApplicationContext.class.getName()); switch (ContextHolder.getCtx().getVersion().getVersion()) { case DSTU2: theSce.getServletContext().setInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, FhirServerConfig.class.getName()); break; case DSTU3: theSce.getServletContext().setInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, FhirServerConfigDstu3.class.getName()); break; case R4: theSce.getServletContext().setInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, FhirServerConfigR4.class.getName()); break; case DSTU2_1: case DSTU2_HL7ORG: break; } cll.contextInitialized(theSce); }