How to Use spring's scope:
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:
Or
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. It is the responsibility of the client code to clear objects in the prototype scope and release expensive resources held by any prototype bean. (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 .)
Configuration instance:
Or
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 first make the following configuration in the web. xml file that initializes the Web:
If you are using a Web Container of servlet 2.4 or later, you only need to add the following contextlistener in the XML declaration file web. xml of the Web application:
... Org. springframework. Web. Context. Request. requestcontextlistener ...
If it is a Web Container before servlet2.4, you need to use a javax. servlet. Filter Implementation:
.. Requestcontextfilter Org. springframework. Web. Filter. requestcontextfilter Requestcontextfilter /* ...
Then you can configure the bean scope:
4. Session
Session scope indicates that a new bean is generated for each HTTP request. The bean is only valid in the current HTTP session. Configure the instance:
Configuration instance:
Similar to the prerequisite for configuring an instance for request, you can configure the Web startup file as follows:
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.
Configuration instance:
Similar to the prerequisite for configuring an instance for request, you can configure the Web startup file as follows:
6. Custom bean Assembly Scope
In spring 2.0, the scope can be expanded at will. You can customize the scope, or even redefine the existing scope (but you cannot overwrite singleton and prototype ), the scope of spring is defined by the org. springframework. beans. factory. config. scope. to customize your own scope, you only need to implement this interface. The following is an example:
We create a thread scope, which is valid in a thread. The Code is as follows:
Publicclass myscope implements scope...
{
Private Final threadlocal threadscope = new threadlocal ()...
{Protected object initialvalue ()... {returnnew hashmap () ;}}; public object get (string name, objectfactory )... {map scope = (MAP) threadscope. get (); object = scope. get (name); If (Object = NULL )... {object = objectfactory. getObject (); scope. put (name, object);} return object;} public object remove (string name )... {map scope = (MAP) threadscope. get (); Return scope. remove (name);} publicvoid registerdestructioncallback (string name, runnable callback )... {} Public String getconversationid ()... {// todo auto-generated method stubreturnnull ;}}