Scope of Spring Bean

Source: Internet
Author: User

1. Two types of scopes commonly used

? Scope= "Singleton"

? Scope= "Prototype"

First, scope= "Singleton" is the default scope for the spring IOC container to create the bean, which means that each bean is created only once and the state of the bean is saved. Then take the same bean from the IOC container again and take it directly, without having to create it again.

Then, scope= "prototype" means that each time you take the same bean from the IOC container, it creates a new BEAN,IOC container that does not save the state of the bean.


2. How to use

The use of the two scopes is subject to certain principles, namely:

? Stateful beans use the scope= "prototype" scope to prevent thread-safety problems under multithreading.

? Stateless beans take the default scope= "singleton" scope, because first it does not have a thread safety problem, second, so that you can share instances, improve program performance.

Specifically:

? STRUTS2 action because there will be user, bizentity such instance object, is stateful information, in a multithreaded environment is not secure. So, in spring, in Struts2 's action, scope is to be prototype scoped.

? The service layer, the DAO layer with the default singleton line, although the service class also has a DAO such properties, but DAO these classes are no state information, that is, equivalent to the invariant (immutable) class, so it does not affect.

The concepts of "thread safety" and "stateful, stateless" are explained below.


3. Thread Safety

When doing something to a complex object, from the beginning of the operation to the end of the operation, the object being manipulated tends to go through several illegal intermediate states. Invoking a function (assuming the function is correct) often causes the object to temporarily fall into an unusable state (often referred to as an unstable state), and the object will not return to the fully usable state until the operation has completely ended. If another thread attempts to access an object that is in an unavailable state, the object will not respond correctly and produce unpredictable results.

By the above, thread safety is a problem that can be caused by multiple threads while manipulating an object at the same time. In other words, thread safety problems are basically caused by global variables and static variables .

In general, this global variable is thread-safe if there are only read operations for global variables and static variables in each thread, and if multiple threads are concurrently performing write operations, it is generally necessary to consider thread synchronization, which may affect thread safety.

Can be seen

? Constants are always thread-safe because only read operations exist.

? It is thread-safe to create a new instance each time the method is called, because the shared resource is not accessed.

? Local variables are thread-safe. Because each execution of a method creates a local variable in a separate space, it is not a shared resource. Local variables include the parameter variables of the method and the in-method variables.


4. Stateful & Stateless

First, we often use the HTTP protocol is stateless, the browser after the server will be disconnected immediately, will not be attached to the server. The server does not save the status information for the client, so the cookie and session appear in order to maintain the HTTP connection status. And, stateful protocols, such as the UDP protocol, from the CS game we used to play, we can see that the client and the server is always connected, otherwise, if the server is disconnected, wait until the server again, long to play dead.

Personal feeling, the state information of an object is mainly embodied by its attributes.

? There is a state is a data storage function, you can save data, you can read and write its properties, to distinguish the same class of two different objects. It is non-thread safe. No state is preserved between different method invocations.

? Stateless is a single operation, can not save data, is immutable class, is thread-safe.

? In short, if more than one object of a class can be separated, then it is stateful; Conversely, if multiple objects of a class cannot be distinguished, then it is said to be stateless.

A bean has no member variables, or the bean's member variable is a stateless bean, then the bean is stateless. Usually such objects are "Operation" classes, which are all operating methods, such as service layer, DAO layer class. The reverse is a stateful bean.


5. Summary

Scope= "Singleton" is the default scope for sping to create a bean, and only one shared bean instance exists in the same ID container. This allows you to share instances and improve performance. It applies to "stateless beans". In order to solve the thread safety problem of "stateful bean" in multi-threaded environment, scope= "prototype", which does not share the instance, will create a new bean each time. General Service layer, DAO layer class are stateless, take the default scope, and the action is generally stateful, so you need to set the Scope= "prototype" property.

Scope of Spring Bean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.