Spring为什么不提供线程范围实现? [英] Why doesn't spring provide a thread scope implementation?

查看:97
本文介绍了Spring为什么不提供线程范围实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring为什么不提供线程范围实现? 有人在Web应用程序上下文中使用过Spring的线程范围的bean吗? 应该有一个标准的,清楚的说明如何执行此操作! (SpringByExample有一个解决方案-我没有测试它-但它还不是主流.)

Why doesn't Spring provide a thread scope implementation? Has anyone used thread scoped beans with Spring in a web application context? There should be a standard, clear description of how to do this! (SpringByExample has a solution—I didn't test it—but it is not mainstream yet.)

推荐答案

Spring确实提供了线程作用域,但默认情况下未注册.

Spring does provide a thread scope, but it is not registered by default.

现有的bean范围在文档此处.

The existing bean scopes are defined in the documentation, here.

单个

  • (默认),每个Spring IoC容器将单个bean定义的作用域限定为单个对象实例.

原型

  • 将单个bean定义的作用域范围扩展到任意数量的对象 实例.
  • Scopes a single bean definition to any number of object instances.

请求

  • 将单个bean定义的作用域限定为 一个HTTP请求;也就是说,每个HTTP请求都有其自己的实例 在单个bean定义的后面创建的bean的视图.仅有效 在具有网络意识的Spring ApplicationContext的上下文中.
  • Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

会话

  • 将单个bean定义的作用域限定为HTTP会话的生命周期. 仅在可感知网络的Spring ApplicationContext上下文中有效. 全局
  • Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. global

应用

  • 将单个bean定义的作用域限定为ServletContext的生命周期.仅在基于Web的Spring上下文中有效 ApplicationContext.
  • Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.

网络套接字

  • 将单个bean定义的作用域限定为WebSocket的生命周期.仅有的 在可感知网络的Spring ApplicationContext上下文中有效.
  • Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

文档随后进行记录

从Spring 3.0开始,线程作用域可用,但未注册 默认情况下.有关更多信息,请参见 SimpleThreadScope .

As of Spring 3.0, a thread scope is available, but is not registered by default. For more information, see the documentation for SimpleThreadScope.

请注意,类似于原型作用域,线程作用域

Note that, similarly to the prototype scope, the thread scope

[SimpleThreadScope]不会清除与其关联的任何对象.

[SimpleThreadScope] does not clean up any objects associated with it.

此线程作用域实现使用ThreadLocal存储bean. 您无法在Java中检测到Thread结尾/死亡,因此Spring IOC容器无法显式地知道何时从ThreadLocal中删除bean并调用生命周期的任何结尾方法.然后,这种责任就落在了开发人员身上.

This thread scope implementation uses a ThreadLocal to store beans. You cannot detect a Thread ending/dying in Java, so the Spring IOC container cannot explicitly know when to remove the beans from the ThreadLocal and invoke any end of lifecycle methods. That responsibility then falls on the developer.

在使用此示波器时要小心.例如,在线程池上下文中,可能已经创建了一个bean,并将其存储在池的重用线程之一中.根据您的用例,这可能是不正确的行为.

Be careful where you use this scope. For example, in a thread pooling context, a bean might already have been created and stored in one of the pools' reused threads. Depending on your use case, that might be the incorrect behavior.

这篇关于Spring为什么不提供线程范围实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆