栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

一个applicationContext 加载错误导致的阻塞问题及解决方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

一个applicationContext 加载错误导致的阻塞问题及解决方法

问题为对接一个sso的验证模块,正确的对接姿势为,接入一个 filter, 然后接入一个 SsoListener 。

  然而在接入之后,却导致了应用无法正常启动,或者说看起来很奇怪,来看下都遇到什么样的问题,以及是如何处理的?

还是 web.xml, 原本是这样的: (很简洁!)



 xx-test
 
  encodingFilter
  org.springframework.web.filter.CharacterEncodingFilter
  
   encoding
   UTF-8
  
  
   forceEncoding
   true
  
 
 
  encodingFilter
  
  @Override
  public void contextInitialized(ServletContextEvent event) {
    initWebApplicationContext(event.getServletContext());
  }

  也就是说,只要把这个配置放到新增的 filter 之前,即可实现正常情况下的加载!

  验证结果,果然如此!

最后,附上一段 tomcat 加载 context 的鲁棒代码,以供参考:


  public boolean listenerStart() {
    if (log.isDebugEnabled())
      log.debug("Configuring application event listeners");
    // Instantiate the required listeners
    String listeners[] = findApplicationListeners();
    Object results[] = new Object[listeners.length];
    boolean ok = true;
    for (int i = 0; i < results.length; i++) {
      if (getLogger().isDebugEnabled())
 getLogger().debug(" Configuring event listener class '" +
   listeners[i] + "'");
      try {
 String listener = listeners[i];
 results[i] = getInstanceManager().newInstance(listener);
      } catch (Throwable t) {
 t = ExceptionUtils.unwrapInvocationTargetException(t);
 ExceptionUtils.handleThrowable(t);
 getLogger().error(sm.getString(
     "standardContext.applicationListener", listeners[i]), t);
 ok = false;
      }
    }
    if (!ok) {
      getLogger().error(sm.getString("standardContext.applicationSkipped"));
      return false;
    }
    // Sort listeners in two arrays
    ArrayList eventListeners = new ArrayList<>();
    ArrayList lifecycleListeners = new ArrayList<>();
    for (int i = 0; i < results.length; i++) {
      if ((results[i] instanceof ServletContextAttributeListener)
 || (results[i] instanceof ServletRequestAttributeListener)
 || (results[i] instanceof ServletRequestListener)
 || (results[i] instanceof HttpSessionIdListener)
 || (results[i] instanceof HttpSessionAttributeListener)) {
 eventListeners.add(results[i]);
      }
      if ((results[i] instanceof ServletContextListener)
 || (results[i] instanceof HttpSessionListener)) {
 lifecycleListeners.add(results[i]);
      }
    }
    // Listener instances may have been added directly to this Context by
    // ServletContextInitializers and other code via the pluggability APIs.
    // Put them these listeners after the ones defined in web.xml and/or
    // annotations then overwrite the list of instances with the new, full
    // list.
    for (Object eventListener: getApplicationEventListeners()) {
      eventListeners.add(eventListener);
    }
    setApplicationEventListeners(eventListeners.toArray());
    for (Object lifecycleListener: getApplicationLifecycleListeners()) {
      lifecycleListeners.add(lifecycleListener);
      if (lifecycleListener instanceof ServletContextListener) {
 noPluggabilityListeners.add(lifecycleListener);
      }
    }
    setApplicationLifecycleListeners(lifecycleListeners.toArray());
    // Send application start events
    if (getLogger().isDebugEnabled())
      getLogger().debug("Sending application start events");
    // Ensure context is not null
    getServletContext();
    context.setNewServletContextListenerAllowed(false);
    Object instances[] = getApplicationLifecycleListeners();
    if (instances == null || instances.length == 0) {
      return ok;
    }
    ServletContextEvent event = new ServletContextEvent(getServletContext());
    ServletContextEvent tldEvent = null;
    if (noPluggabilityListeners.size() > 0) {
      noPluggabilityServletContext = new NoPluggabilityServletContext(getServletContext());
      tldEvent = new ServletContextEvent(noPluggabilityServletContext);
    }
    for (int i = 0; i < instances.length; i++) {
      if (!(instances[i] instanceof ServletContextListener))
 continue;
      ServletContextListener listener =
 (ServletContextListener) instances[i];
      try {
 fireContainerEvent("beforeContextInitialized", listener);
 // 调用 listener.contextInitialized() 触发 listener
 if (noPluggabilityListeners.contains(listener)) {
   listener.contextInitialized(tldEvent);
 } else {
   listener.contextInitialized(event);
 }
 fireContainerEvent("afterContextInitialized", listener);
      } catch (Throwable t) {
 ExceptionUtils.handleThrowable(t);
 fireContainerEvent("afterContextInitialized", listener);
 getLogger().error
   (sm.getString("standardContext.listenerStart",
   instances[i].getClass().getName()), t);
 ok = false;
      }
    }
    return (ok);
  }

总结

以上所述是小编给大家介绍的一个applicationContext 加载错误导致的阻塞问题及解决方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号