javax.servlet.ServletContextListener.contextInitialized()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(207)

本文整理了Java中javax.servlet.ServletContextListener.contextInitialized()方法的一些代码示例,展示了ServletContextListener.contextInitialized()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ServletContextListener.contextInitialized()方法的具体详情如下:
包路径:javax.servlet.ServletContextListener
类名称:ServletContextListener
方法名:contextInitialized

ServletContextListener.contextInitialized介绍

[英]Receives notification that the web application initialization process is starting.

All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.
[中]接收web应用程序初始化过程正在启动的通知。
在初始化web应用程序中的任何筛选器或servlet之前,会通知所有ServletContextListeners上下文初始化。

代码示例

代码示例来源:origin: org.freemarker/freemarker

public void contextInitialized(ServletContextEvent arg0) {
  arg0.getServletContext().setAttribute(ATTR_NAME, this);
  
  synchronized (servletContextListeners) {
    int s = servletContextListeners.size();
    for (int i = 0; i < s; ++i) {
      ((ServletContextListener) servletContextListeners.get(i)).contextInitialized(arg0);
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public <T extends EventListener> void addListener(T t) {
  if (t instanceof ServletContextListener) {
    ((ServletContextListener) t).contextInitialized(new ServletContextEvent(this));
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Addresses the issues raised in <a
 * href="http://opensource.atlassian.com/projects/spring/browse/SPR-4008"
 * target="_blank">SPR-4008</a>: <em>Supply an opportunity to customize
 * context before calling refresh in ContextLoaders</em>.
 */
@Test
public void testContextLoaderListenerWithCustomizedContextLoader() {
  final StringBuffer buffer = new StringBuffer();
  final String expectedContents = "customizeContext() was called";
  final MockServletContext sc = new MockServletContext("");
  sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
      "/org/springframework/web/context/WEB-INF/applicationContext.xml");
  ServletContextListener listener = new ContextLoaderListener() {
    @Override
    protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) {
      assertNotNull("The ServletContext should not be null.", sc);
      assertEquals("Verifying that we received the expected ServletContext.", sc, sc);
      assertFalse("The ApplicationContext should not yet have been refreshed.", wac.isActive());
      buffer.append(expectedContents);
    }
  };
  listener.contextInitialized(new ServletContextEvent(sc));
  assertEquals("customizeContext() should have been called.", expectedContents, buffer.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@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"));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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);
  }
}

代码示例来源:origin: awslabs/aws-serverless-java-container

private void notifyStartListeners(ServletContext context) {
  for (ServletContextListener listener : contextListeners) {
    listener.contextInitialized(new ServletContextEvent(context));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@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));
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

servletContext);
servletContextListener.contextInitialized(servletContextEvent);

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testContextDestroyed() {
  ServletContext context = mock(ServletContext.class);
  ServletContextEvent destroyed = mock(ServletContextEvent.class);
  when(destroyed.getServletContext()).thenReturn(context);
  ServletContextListener listener = new IIOProviderContextListener();
  listener.contextInitialized(mock(ServletContextEvent.class));
  listener.contextDestroyed(destroyed);
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testContextInitialized() {
  ServletContextListener listener = new IIOProviderContextListener();
  listener.contextInitialized(mock(ServletContextEvent.class));
}

代码示例来源:origin: org.tinygroup/org.tinygroup.weblayerbase

private void startListeners() {
  for (ServletContextListener listener : listeners) {
    listener.contextInitialized(event);
  }
}

代码示例来源:origin: com.vaadin/flow-server

@Override
public void contextInitialized(ServletContextEvent sce) {
  for (ServletContextListener listener : listeners) {
    listener.contextInitialized(sce);
  }
}

代码示例来源:origin: org.apache.myfaces.test/myfaces-test22

public void contextInitialized(ServletContextEvent sce)
{
  if (contextListeners != null && !contextListeners.isEmpty())
  {
    for (ServletContextListener listener : contextListeners)
    {
      listener.contextInitialized(sce);
    }
  }
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testDestroyConcurrentModRegression() {
  ServletContext context = mock(ServletContext.class);
  ServletContextEvent destroyed = mock(ServletContextEvent.class);
  when(destroyed.getServletContext()).thenReturn(context);
  ServletContextListener listener = new IIOProviderContextListener();
  listener.contextInitialized(mock(ServletContextEvent.class));
  ImageReaderSpi provider1 = new MockImageReaderSpiOne();
  ImageReaderSpi provider2 = new MockImageReaderSpiToo();
  // NOTE: Fake registering for simplicity, but it still exposes the original problem with de-registering
  IIORegistry registry = IIORegistry.getDefaultInstance();
  registry.registerServiceProvider(provider1);
  registry.registerServiceProvider(provider2);
  assertTrue(registry.contains(provider1));
  assertTrue(registry.contains(provider2));
  listener.contextDestroyed(destroyed);
  assertFalse(registry.contains(provider1));
  assertFalse(registry.contains(provider2));
}

代码示例来源:origin: org.freemarker/com.springsource.freemarker

public void contextInitialized(ServletContextEvent arg0)
{
  arg0.getServletContext().setAttribute(ATTR_NAME, this);
  
  synchronized(servletContextListeners)
  {
    int s = servletContextListeners.size();
    for(int i = 0; i < s; ++i)
    {
      ((ServletContextListener)servletContextListeners.get(i)).contextInitialized(arg0);
    }
  }
}

代码示例来源:origin: com.haulmont.cuba/cuba-web

protected void initWebServletContextListener(ServletContextEvent servletContextEvent, ServletContext sc) {
  String className = sc.getInitParameter("webServletContextListener");
  if (!Strings.isNullOrEmpty(className)) {
    try {
      Class<?> clazz = this.getClass().getClassLoader().loadClass(className);
      webServletContextListener = (ServletContextListener) clazz.newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
      throw new RuntimeException("An error occurred while starting single WAR application", e);
    }
    webServletContextListener.contextInitialized(servletContextEvent);
  }
}

代码示例来源:origin: com.ovea.tajin.servers/tajin-server-jetty9

protected void callContextInitialized (ServletContextListener l, ServletContextEvent e)
{
  LOG.debug("contextInitialized: {}->{}",e,l);
  l.contextInitialized(e);
}

代码示例来源:origin: sonatype/nexus-public

@Override
public ServletContextListener addingService(ServiceReference<ServletContextListener> reference) {
 ServletContextListener service = super.addingService(reference);
 service.contextInitialized(new ServletContextEvent(servletContext));
 return service;
}

相关文章