Java Code Examples for javax.servlet.ServletContextListener#contextInitialized()

The following examples show how to use javax.servlet.ServletContextListener#contextInitialized() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProxyServletListener.java    From lemon with Apache License 2.0 6 votes vote down vote up
public void contextInitialized(ServletContextEvent sce) {
    ctx = WebApplicationContextUtils.getWebApplicationContext(sce
            .getServletContext());

    if (ctx == null) {
        logger.warn("cannot find applicationContext");

        return;
    }

    Collection<ServletContextListener> servletContextListeners = ctx
            .getBeansOfType(ServletContextListener.class).values();

    for (ServletContextListener servletContextListener : servletContextListeners) {
        servletContextListener.contextInitialized(sce);
    }
}
 
Example 2
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testContextLoaderWithInvalidContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.context.support.InvalidWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown ApplicationContextException");
	}
	catch (ApplicationContextException ex) {
		// expected
		assertTrue(ex.getCause() instanceof ClassNotFoundException);
	}
}
 
Example 3
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testContextLoaderWithInvalidContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.context.support.InvalidWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown ApplicationContextException");
	}
	catch (ApplicationContextException ex) {
		// expected
		assertTrue(ex.getCause() instanceof ClassNotFoundException);
	}
}
 
Example 4
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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));
}
 
Example 5
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testContextLoaderWithInvalidContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.context.support.InvalidWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown ApplicationContextException");
	}
	catch (ApplicationContextException ex) {
		// expected
		assertTrue(ex.getCause() instanceof ClassNotFoundException);
	}
}
 
Example 6
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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));
}
 
Example 7
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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);
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	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(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
	assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
 
Example 8
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderWithDefaultLocation() throws Exception {
	MockServletContext sc = new MockServletContext("");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
		assertTrue(ex.getCause() instanceof IOException);
		assertTrue(ex.getCause().getMessage().contains("/WEB-INF/applicationContext.xml"));
	}
}
 
Example 9
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextLoaderWithDefaultLocation() throws Exception {
	MockServletContext sc = new MockServletContext("");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
		assertTrue(ex.getCause() instanceof IOException);
		assertTrue(ex.getCause().getMessage().contains("/WEB-INF/applicationContext.xml"));
	}
}
 
Example 10
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextLoaderWithInvalidLocation() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
		assertTrue(ex.getCause() instanceof FileNotFoundException);
	}
}
 
Example 11
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextLoaderWithCustomContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.servlet.SimpleWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	assertTrue("Correct WebApplicationContext exposed in ServletContext", wc instanceof SimpleWebApplicationContext);
}
 
Example 12
Source File: ContextLoaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextLoaderWithDefaultContextAndParent() throws Exception {
	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");
	sc.addInitParameter(ContextLoader.LOCATOR_FACTORY_SELECTOR_PARAM,
			"classpath:org/springframework/web/context/ref1.xml");
	sc.addInitParameter(ContextLoader.LOCATOR_FACTORY_KEY_PARAM, "a.qualified.name.of.some.sort");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	assertTrue("Correct WebApplicationContext exposed in ServletContext",
			context 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());
	assertTrue(context.containsBean("beans1.bean1"));
	assertTrue(context.isTypeMatch("beans1.bean1", org.springframework.beans.factory.access.TestBean.class));
	assertTrue(context.containsBean("beans1.bean2"));
	assertTrue(context.isTypeMatch("beans1.bean2", org.springframework.beans.factory.access.TestBean.class));
	listener.contextDestroyed(event);
	assertTrue("Destroyed", lb.isDestroyed());
}
 
Example 13
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderWithInvalidLocation() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
		assertTrue(ex.getCause() instanceof FileNotFoundException);
	}
}
 
Example 14
Source File: ContextLoaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderWithCustomContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.servlet.SimpleWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(contextAttr);
	assertTrue("Correct WebApplicationContext exposed in ServletContext",
			wc instanceof SimpleWebApplicationContext);
}
 
Example 15
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderWithDefaultLocation() throws Exception {
	MockServletContext sc = new MockServletContext("");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
		assertTrue(ex.getCause() instanceof IOException);
		assertTrue(ex.getCause().getMessage().contains("/WEB-INF/applicationContext.xml"));
	}
}
 
Example 16
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderWithInvalidLocation() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	try {
		listener.contextInitialized(event);
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
		assertTrue(ex.getCause() instanceof FileNotFoundException);
	}
}
 
Example 17
Source File: ContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testContextLoaderWithCustomContext() throws Exception {
	MockServletContext sc = new MockServletContext("");
	sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
			"org.springframework.web.servlet.SimpleWebApplicationContext");
	ServletContextListener listener = new ContextLoaderListener();
	ServletContextEvent event = new ServletContextEvent(sc);
	listener.contextInitialized(event);
	String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
	WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(contextAttr);
	assertTrue("Correct WebApplicationContext exposed in ServletContext",
			wc instanceof SimpleWebApplicationContext);
}
 
Example 18
Source File: ListenerTracker.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public ServletContextListener addingService(ServiceReference<ServletContextListener> reference) {
  ServletContextListener service = super.addingService(reference);
  service.contextInitialized(new ServletContextEvent(servletContext));
  return service;
}
 
Example 19
Source File: ServletContextListeners.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public void contextInitialized(ServletContextEvent sce) {
    for (ServletContextListener listener : listeners) {
        listener.contextInitialized(sce);
    }
}