素材牛VIP会员
ServletContext 和 spring ApplicationContext 的线程问题
 冷***知  分类:Java代码  人气:639  回帖:1  发布于6年前 收藏

看到 spring 的 ServeltContextListener 加载方式时,有个问题。

不知道我的理解对不对:

tomcat 下一个 web 应用目录对应一个 ServeltContext 单例,假设使用 Servelt 处理请求,那一个 Servelt class 其实在 tomcat 容器中也是单例,但是为了提升并发性能,这个单例可能会被多个线程使用。

再到 spring ,在 servlet 被实例化这一步之前, spring 的 ContextLoaderListener 监听器被调用,通过 org.springframework.web.context.ContextLoader 创建 ApplicationContext 对象

问题主要在 ApplicartionContext 这个地方:

这个应用上下文是多线程的吗?因为看到了 static volatile 的 currentContext 和 ConcurrentHashMap 的 currentContextPerThread ,但是为什么还要保存一份当前线程 ContextClassLoader 管理的 WebApplicationContext ?直接用 Heap 区的那一份 currentContext 不行吗?还有,其它线程的 WebApplicationContext 又是在什么时机被创建出来的呢?

讨论这个帖子(1)垃圾回帖将一律封号处理……

Lv6 码匠
骨***活 产品经理 6年前#1
 文明上网,理性发言!   😉 阿里云幸运券,戳我领取