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.
<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. 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:
<Bean id = "role" class = "Spring. chapter2.marygame. role" Scope = "prototype"/>
Or
<Beanid = "role" class = "Spring. chapter2.marygame. role" Singleton = "false"/>
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:
<Web-app>
...
<Listener>
<Listener-class> org. springframework. Web. Context. Request. requestcontextlistener </listener-class>
</Listener>
...
</Web-app>
If it is a Web Container before servlet2.4, you need to use a javax. servlet. Filter Implementation:
<Web-app>
..
<Filter>
<Filter-Name> requestcontextfilter </filter-Name>
<Filter-class> org. springframework. Web. Filter. requestcontextfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> requestcontextfilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
...
</Web-app>
Then you can configure the bean scope:
<Bean id = "role" class = "Spring. chapter2.marygame. role" Scope = "request"/>
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:
<Bean id = "role" class = "Spring. chapter2.marygame. role" Scope = "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.
Configuration instance:
Similar to the prerequisite for configuring an instance for request, you can configure the Web startup file as follows:
<Bean id = "role" class = "Spring. chapter2.marygame. role" Scope = "global session"/>
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... {
Privatefinal threadlocal threadscope = new threadlocal ()... {
Protected object initialvalue ()... {
Returnnew hashmap ();
}
};
Public object get (string name, objectfactory )... {
Map scope = (MAP) threadscope. Get ();
Object 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 stub
Returnnull;
}
}