Spring Bean scope, springbean
1. Two common scopes
Scope = "singleton"
Scope = "prototype"
Scope = "singleton" is the default scope of Bean created by Spring's IoC container. It indicates that each Bean is created only once and the Bean status is saved, you can use the same Bean from the IoC container again without creating it again.
Then, scope = "prototype" indicates that every time you get the same Bean from the IoC container, a new Bean will be created, and the IoC container will not save the Bean status.
2. Usage
The use of the two scopes must follow certain principles:
Ø stateful beans use scope = "prototype" scope to prevent thread security problems under multiple threads.
The stateless Bean adopts the default scope = "singleton" scope, because it does not have thread security problems first, and secondly, it can share instances to improve program performance.
For details, see:
The Action in Struts2 contains instance objects such as User and BizEntity, which have State information and are insecure in multi-threaded environments. Therefore, in Spring, in Struts2 Action, scope must be configured with prototype scope.
The default singleton is used for the Service layer and Dao layer. Although the Service class also has the dao attribute, the dao class has no status information, that is, immutable) class, so it does not affect.
The following describes the concepts of "thread security" and "stateful and stateless.
3. Thread Security
When performing an operation on a complex object, the object to be operated often goes through several illegal intermediate states from the beginning to the end of the operation. Calling a function (assuming the function is correct) to operate on an object often causes the object to be temporarily unavailable (usually known as an unstable state). When the operation is complete, this object will return to the fully available state. If other threads attempt to access an object in the unavailable state, the object will not respond correctly, resulting in unexpected results.
As we can see from the above, the thread security issue may be caused by multiple threads and operations on an object at the same time. That is to say, thread security issues are basically caused by global variables and static variables.
If each thread only performs read operations on global variables and static variables without write operations, this global variable is generally thread-safe. If multiple threads execute write operations at the same time, generally, thread synchronization needs to be considered; otherwise, thread security may be affected.
We can see that:
Constants are always thread-safe because only read operations exist.
Creating an instance before each method call is thread-safe because it does not access shared resources.
Local variables are thread-safe. Each time a method is executed, a local variable is created in an independent space, which is not a shared resource. Local variables include the parameter variables of the method and the internal variables of the method.
4. stateful & stateless
First of all, the Http protocol we often use is stateless, and the server will be disconnected immediately after the browser requests, and will not be connected to the server all the time. The server does not save the client status information, so cookies and sessions appear to maintain the Http connection status. In stateful protocols, such as UDP, we can see from the csgames we used to play that the client and the server are always connected. Otherwise, if the server is disconnected, when I connect to the server again, I will be dead.
Personal feeling: the State information of an object is mainly reflected by its attributes.
A stateful object is a data storage function that can store data. You can read and write its attributes to distinguish two different objects of the same class. It is non-thread-safe. No status is retained between different method calls.
The stateless operation is a thread-safe operation. data cannot be saved, and the class is unchanged.
All in all, if multiple objects of a class can be separated, they are stateful. Otherwise, multiple objects of a class cannot be separated, it is stateless.
If a Bean has no member variable or its member variable is stateless, the Bean is stateless. Normally, such objects are some "operation" classes, which are some operation methods, such as services layer and Dao layer classes. Otherwise, there is a stateful Bean.
5. Summary
Scope = "singleton" is the default scope of the Bean created by Sping. Only one shared Bean instance exists in the same id container. In this way, instances can be shared to improve performance. It applies to "stateless Bean ". To solve the thread security problem of "stateful Bean" in a multi-threaded environment, scope = "prototype" appears. It does not share instances and creates a new Bean every time. Generally, the Service layer and Dao layer classes are stateless and use the default scope, while actions are stateful. Therefore, you need to set the scope = "prototype" attribute.