简体   繁体   中英

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:

As a rule, you should use the prototype scope for all stateful beans and the singleton scope for stateless beans.

This is the link to the related section .

Is it because of thread safety that we should not use singleton scope for stateful beans?

Note about the above question: I can understand using singleton scope for stateless beans. Stateless beans are immutable and they are thread-safe by definition. So, using singleton scope for those beans would be a reasonable choice. 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. 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? 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. As far as I know, Spring creates only one instance for Singleton and does not provide thread safety for singleton. In the context of Spring, all singletons may not necessarily be stateful or stateless.

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/

I have been reading Spring Framework's official documentation and I came across this sentence:

As a rule, you should use the prototype scope for all stateful beans and the singleton scope for stateless beans.

This is the link to the related section .

Is it because of thread safety that we should not use singleton scope for stateful beans?

Note about the above question: I can understand using singleton scope for stateless beans. Stateless beans are immutable and they are thread-safe by definition. So, using singleton scope for those beans would be a reasonable choice. 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. 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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