简体   繁体   English

Spring 的原型和单例作用域 bean 关于有状态

[英]Spring's prototype and singleton scoped beans with regards to statefulness

I have been reading Spring Framework's official documentation and I came across this sentence:我一直在阅读 Spring Framework 的官方文档,我遇到了这句话:

As a rule, you should use the prototype scope for all stateful beans and the singleton scope for stateless beans.通常,您应该对所有有状态 bean 使用原型作用域,对无状态 bean 使用单例作用域。

This is the link to the related section .这是相关部分的链接。

Is it because of thread safety that we should not use singleton scope for stateful beans?是因为线程安全,我们不应该对有状态 bean 使用单例作用域吗?

Note about the above question: I can understand using singleton scope for stateless beans.关于上述问题的注意事项:我可以理解对无状态 bean 使用单例范围。 Stateless beans are immutable and they are thread-safe by definition.无状态 bean 是不可变的,并且根据定义它们是线程安全的。 So, using singleton scope for those beans would be a reasonable choice.因此,对这些 bean 使用单例范围将是一个合理的选择。 I know global state is considered evil and using singleton scope for stateful beans (I assume those beans can also be mutable) can cause undesired effects.我知道全局状态被认为是邪恶的,并且对有状态 bean 使用单例范围(我假设这些 bean 也可以是可变的)会导致不良影响。 What caused trouble for me was that these ideas were stated as a rule in the documentation.给我带来麻烦的是,这些想法在文档中被规定为规则 What keeps someone to use singleton scope for stateful beans in a single threaded environment, for instance?例如,是什么让某人在单线程环境中对有状态 bean 使用单例作用域? I know, it is still not a very good idea, but anyway.我知道,这仍然不是一个好主意,但无论如何。 The point of the question is to know if there is rationale behind this rule other than thread-safety.问题的重点是要知道这条规则背后是否有线程安全以外的理由。

There are two things here, one is about state and another is about thread safety.这里有两件事,一个是关于状态的,另一个是关于线程安全的。 All singleton classes may not necessarily be thread-safe until you design it.在您设计之前,所有单例类都不一定是线程安全的。 The states can be maintained in both Singleton and Prototype scope.状态可以在 Singleton 和 Prototype 范围内维护。 As far as I know, Spring creates only one instance for Singleton and does not provide thread safety for singleton.据我所知,Spring只为Singleton创建了一个实例,并没有为Singleton提供线程安全。 In the context of Spring, all singletons may not necessarily be stateful or stateless.在 Spring 的上下文中,所有的单例都不一定是有状态的或无状态的。

You can refer below the link for singleton thread safety.您可以参考下面的单例线程安全链接。 Other may give better suggestions to this question.其他人可能会对这个问题提出更好的建议。

http://dolszewski.com/spring/spring-bean-thread-safety-guide/ http://dolszewski.com/spring/spring-bean-thread-safety-guide/

I have been reading Spring Framework's official documentation and I came across this sentence:我一直在阅读 Spring Framework 的官方文档,遇到了这句话:

As a rule, you should use the prototype scope for all stateful beans and the singleton scope for stateless beans.通常,您应该对所有有状态 bean 使用原型 scope,对无状态 bean 使用 singleton scope。

This is the link to the related section .这是相关部分的链接。

Is it because of thread safety that we should not use singleton scope for stateful beans?是否因为线程安全,我们不应该将 singleton scope 用于有状态 bean?

Note about the above question: I can understand using singleton scope for stateless beans.关于上述问题的注意事项:我可以理解将 singleton scope 用于无状态 bean。 Stateless beans are immutable and they are thread-safe by definition.无状态 bean 是不可变的,根据定义它们是线程安全的。 So, using singleton scope for those beans would be a reasonable choice.因此,对这些 bean 使用 singleton scope 将是一个合理的选择。 I know global state is considered evil and using singleton scope for stateful beans (I assume those beans can also be mutable) can cause undesired effects.我知道全局 state 被认为是邪恶的,并且将 singleton scope 用于有状态的 bean(我假设这些 bean 也可以是可变的)可能会导致不良影响。 What caused trouble for me was that these ideas were stated as a rule in the documentation.给我带来麻烦的是,这些想法在文档中作为一项规则进行了陈述。 What keeps someone to use singleton scope for stateful beans in a single threaded environment, for instance?例如,是什么让人们在单线程环境中将 singleton scope 用于有状态 bean? I know, it is still not a very good idea, but anyway.我知道,这仍然不是一个好主意,但无论如何。 The point of the question is to know if there is rationale behind this rule other than thread-safety.问题的关键是要知道除了线程安全之外,这条规则背后是否还有其他理由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM