我可以发现了一个SpringMVC框架的Bug,描述如下:

本文探讨了一个 Spring MVC 应用中出现的两个 WebApplicationContext 上下文同时被初始化的问题,并提供了详细的日志记录及代码片段以帮助理解这一不合理现象。

(1)

配置文件web.xml其中的片段:

<servlet>
   <servlet-name>OOXXappServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/OOXXappServlet/servlet-context.xml</param-value>
   </init-param>
</servlet>

(2)

使用如下代码,获取ServletContext:

WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
ServletContext sc = wac.getServletContext();
if (sc != null) {
   out.println(
         "\n\tWAC getServletContextName\t" + sc.getServletContextName() +
         "\n\tWAC getClassLoader\t" + sc.getClassLoader() +
         "\n\tWAC getClass\t" + sc.getClass() +
         "\n\tWAC getAttributeNames\t" + sc.getAttributeNames().toString() +
         "\n\tWAC getContextPath\t" + sc.getContextPath() +
         "\n\tWAC getInitParameterNames\t" + sc.getInitParameterNames().toString() +
         "\n\tWAC getServerInfo\t" + sc.getServerInfo() +
         "\n\tWAC getSessionCookieConfig\t" + sc.getSessionCookieConfig()
   );

   Enumeration<String> es = sc.getAttributeNames();
   while (es.hasMoreElements()){
      String attr = es.nextElement().toString();
      out.println(
                  attr +
                  "\t:\t" +
                  sc.getAttribute(attr)
      );
   }
}

 

(3)上面代码的执行Log:

WAC getServletContextName     /
WAC getClassLoader    WebAppClassLoader=105146840@64469d8
WAC getClass  class org.eclipse.jetty.webapp.WebAppContext$Context
WAC getAttributeNames java.util.Collections$3@5c9d540c
WAC getContextPath
WAC getInitParameterNames     java.util.Collections$3@126898e2
WAC getServerInfo     jetty/9.4.1.v20170120
WAC getSessionCookieConfig    org.eclipse.jetty.server.session.SessionHandler$CookieConfig@90ed286

    javax.servlet.context.tempdir   :       E:\JavaTryCatch\spring-mvc-showcase\target\tmp
    org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern      :       .*/javax.servlet-[^/]*\.jar$|.*/servlet-api-[^/]*\.jar$|.*javax.servlet.jsp.jstl-[^/]*\.jar|.*taglibs-standard-impl-.*\.jar
    org.eclipse.jetty.util.DecoratedObjectFactory   :       org.eclipse.jetty.util.DecoratedObjectFactory[decorators=3]
    org.springframework.web.servlet.FrameworkServlet.CONTEXT.OOXXappServlet     :       WebApplicationContext for namespace 'OOXXappServlet-servlet': startup date [Mon Jun 12 18:22:41 CST 2017]; parent: Root WebApplicationContext
    org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.SCI   :       WebSocketUpgradeFilter[configuration=org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration@7a191211]
    org.eclipse.jetty.resource.postOverlay  :       file:///E:/JavaTryCatch/spring-mvc-showcase/src/main/webapp/
    org.eclipse.jetty.lifecyleCallbackCollection    :       org.eclipse.jetty.plus.annotation.LifeCycleCallbackCollection@4c92146a
    resourceCache   :       ResourceCache[null,org.eclipse.jetty.servlet.DefaultServlet@1aeb2a92]@1703825835
    org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration :       org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration@7a191211
    org.apache.tomcat.InstanceManager       :       org.apache.tomcat.SimpleInstanceManager@57b55e91
    org.eclipse.jetty.tmpdirConfigured      :       true
    org.eclipse.jetty.server.Executor       :       qtp2189588{STARTED,8<=13<=200,i=7,q=0}
    org.eclipse.jetty.injectionCollection   :       org.eclipse.jetty.plus.annotation.InjectionCollection@bd82149
    org.eclipse.jetty.runAsCollection       :       org.eclipse.jetty.plus.annotation.RunAsCollection@4824251a
    org.apache.jasper.compiler.ELInterpreter        :       org.apache.jasper.compiler.ELInterpreterFactory$DefaultELInterpreter@5fb026bc
    org.eclipse.jetty.containerInitializerStarter   :       org.eclipse.jetty.annotations.ServletContainerInitializersStarter@220caa0d
    org.apache.jasper.compiler.TldCache     :       org.apache.jasper.compiler.TldCache@6814156c
    org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet      :   WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Mon Jun 12 18:22:38 CST 2017]; parent: Root WebApplicationContext
    org.springframework.web.context.WebApplicationContext.ROOT      :       Root WebApplicationContext: startup date [Mon Jun 12 18:22:38 CST 2017]; root of context hierarchy
    org.springframework.web.context.support.ServletContextScope     :       org.springframework.web.context.support.ServletContextScope@171f181
    org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter       :       WebSocketUpgradeFilter[configuration=org.eclipse.jetty.websocket.server.NativeWebSocketConfiguration@7a191211]
    javax.websocket.server.ServerContainer  :       org.eclipse.jetty.websocket.jsr356.server.ServerContainer@5faaa2d5
    org.apache.jasper.runtime.JspApplicationContextImpl     :       org.apache.jasper.runtime.JspApplicationContextImpl@18b127ef
 

(4)问题描述

可以在上面的记录中,attribute里有2个“子上下文”:

org.springframework.web.servlet.FrameworkServlet.CONTEXT.OOXXappServlet 和 org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet

其value分别是:
WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Mon Jun 12 18:22:38 CST 2017]; parent: Root WebApplicationContext
和 WebApplicationContext for namespace 'OOXXappServlet-servlet': startup date [Mon Jun 12 18:22:41 CST 2017]; parent: Root WebApplicationContext 

 

也就是说,同时初始化了2个MVC上下文,这是不合理的啊;所以,应该算是一个Bug吧!

 

 

 

 

转载于:https://my.oschina.net/u/614221/blog/919664

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值