Spring bean Scope

Source: Internet
Author: User

<Bean id = "role" class = "Spring. chapter2.marygame. role" Scope = "Singleton"/>

Scope is used to configure the scope of spring bean, which identifies the scope of bean. Before spring2.0, beans have only two scopes: Singleton (Singleton), non-singleton (also known as prototype), and spring2.0, added three beans for the Web application context: session, request, and global session. Therefore, spring2.0 currently has five types of beans by default. Of course, spring2.0 restructured the bean type design and designs flexible bean type support. In theory, there can be countless types of beans, and users can follow their own needs, add new bean types to meet actual application requirements .!

1. Singleton Scope

When the scope of a bean is set to Singleton, only one shared bean instance exists in the Spring IoC container, and all requests to the bean, as long as the ID matches the bean definition, only the same bean instance is returned. In other words, when a bean definition is set to the singleton scope, the Spring IoC container will only create a unique instance of the bean definition. This single instance will be stored in singleton cache, and all subsequent requests and references to this bean will return the cached object instance, note that Singleton's scope is completely different from that in gof's design pattern. Singleton's design pattern indicates that only one class exists in a classloader, singleton indicates that a container corresponds to a bean. That is to say, when a bean is identified as Singleton, only one bean exists in the IOC container of spring.

Configuration instance:
<Bean id = "role" class = "Spring. chapter2.marygame. role" Scope = "Singleton"/>
Or
<Bean id = "role" class = "Spring. chapter2.marygame. role" Singleton = "true"/>

2. Prototype
The bean deployed in the prototype scope generates a new bean instance for each request (inject it into another bean, or call the getbean () method of the container as a program, similar to a new operation, spring is not responsible for the entire lifecycle of a prototype bean, after the container initializes, configures, decorations, or assembles a prototype instance, it is handed over to the client, and then the prototype instance is ignored. Regardless of the scope, the container will call the initialization lifecycle callback method of all objects. For prototype, no configured destructor lifecycle callback method will be called. Clear the objects in prototype scope and release any prototype.
Bean's expensive resources are the responsibility of client code. (One feasible way for spring containers to release resources occupied by beans in the singleton scope is to use the bean postprocessor to hold the reference of the bean to be purged .)

3. Request

Request indicates that a new bean is generated for each HTTP request, and the bean is only valid in the current HTTP request. Configure the instance: when using request, session, and global session, you must initialize the web. make the following configuration in XML: if you are using a Web Container of servlet 2.4 or later, you only need to declare the file web in the XML file of the Web application. add the following contextlistener to XML:

<Listener>
<Listener-class> org. springframework. Web. Context. Request. requestcontextlistener </listener-class>
</Listener>

4. Session

Session scope indicates that a new bean is generated for each HTTP request, and the bean is valid only in the current HTTP session.

5. Global session
The global session scope is similar to the standard HTTP session scope, but it only makes sense in the Portlet-based Web application. The Portlet Specification defines the concept of a global session, which is shared by all the different Portlet that constitute a Portlet web application. Beans defined in the global session scope are limited to the global Portlet session lifecycle. If you use the global session scope in the Web to identify the bean, the Web will automatically use it as the session type.

6. The custom bean Assembly scope can be arbitrarily expanded in spring. You can customize the scope, you can even redefine the existing scope (but you cannot overwrite singleton and prototype). Spring's scope is defined by the interface Org. springframework. beans. factory. con **. scope. to customize your own scope, you only need to implement this interface.

Discuss singleton and prototype again:

Scope = "prototype" is not written. to add, delete, and delete a table in the project, an action is used. The actionadd, update, delete, and save methods are used, the ADD and modify operations share a page. When the page gets the ID, the table is modified, and vice versa. Because the bean configured with spring forgets to write scope = "prototype", the last accessed record is displayed each time it is added, scope = "prototype" creates a new action object when this type of object is requested. If scope = prototype is not configured, no action will be created during addition, and the information of the last access record will be retained.
The action of webwork is NOT thread-safe. In a multi-threaded environment, it must be a thread corresponding to an independent instance and cannot use Singleton. Therefore, When configuring webwork action bean in spring, we need to add the attribute scope = "prototype" or Singleton = "false ".

Singleton mode refers to the full sharing of an object, including the code space and data space. To put it bluntly, if a class is Singleton, if the class has a member variable, the value of this member variable is shared by various threads (a bit similar to static). After thread a assigns a value to the variable, thread B can read the value. Therefore, for foreground actions, the singleton mode cannot be used. A thread request must correspond to an independent instance. Broadly speaking, Singleton cannot be used as long as it is a class with data member variables to prevent multiple threads from mixing data. For the services and Dao we use, Singleton is used because they do not use data member variables.
Service requires data member variables. Set Singleton to false. Stateful beans use prototype scopes, while stateless beans use Singleton scopes.

In spring2.0, besides singleton and prototype, three new web scopes are added: request, session, and global session. If you want a bean in the container to have a new Web scope, in addition to configuring the corresponding scope attribute at the bean level, you must also perform an additional Initialization Configuration at the container level. Add the contextlistener: org. springframework. Web. Context. Request. requestcontextlistener to the Web. xml file of the Web application.
The above is for Versions later than servlet 2.4. For example, request scope.

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.