Spring 上下文中的有状态 bean 和无状态 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4506241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stateful beans and Stateless beans in Spring context
提问by Umesh Awasthi
I am reading spring through its official documentation and at one place I came to a line that uses prototype scope for all stateful beans while singleton for stateless beans.
我正在阅读 spring 的官方文档,在一个地方,我发现了一条线,它对所有有状态 bean 使用原型范围,而对无状态 bean 使用单例。
I know there is something as stateful as well as stateless beans in EJB but this is not what they have mentioned in the documents.
我知道在 EJB 中存在与有状态和无状态 bean 一样的东西,但这不是他们在文档中提到的。
Can anyone explain to me what exactly this means of stateful as well stateless beans in Spring
谁能向我解释一下 Spring 中有状态和无状态 bean 的确切含义
Thanks in advance
提前致谢
回答by Aravind Yarram
From spring perspective
从春天的角度
stateless beans: beans that are singleton and are initialized only once. The only state they have is a shared state. These beans are created while the
ApplicationContextis being initialized. The SAME bean instance will be returned/injected during the lifetime of thisApplicationContext.stateful beans: beans that can carry state (instance variables). These are created EVERY time an object is required (like using the "new" operator in java).
无状态 bean:单例 bean,仅初始化一次。他们拥有的唯一状态是共享状态。这些 bean 是在
ApplicationContext初始化时创建的。SAME bean 实例将在 this 的生命周期内返回/注入ApplicationContext。有状态bean:可以携带状态(实例变量)的bean。每次需要对象时都会创建这些对象(例如在 Java 中使用“new”运算符)。
These are not EJB statfull/stateless session beans.
这些不是 EJB statfull/stateless session bean。

