Scope of the Bean in spring

Source: Internet
Author: User
Tags http request object object sessions

How to use the scope of spring:
<bean id= "Role" class= "Spring.chapter2.maryGame.Role" scope= "singleton"/>
The scope here is used to configure the scope of the spring bean, which identifies the scope of the bean.
Before spring2.0, the Bean had only 2 scopes: Singleton (single example), Non-singleton (also known as prototype), after Spring2.0, added session, request, global Session three beans dedicated to the context of the Web application. Therefore, by default Spring2.0 now has five types of beans. Of course, Spring2.0 the design of the bean's type and designed a flexible bean type support, theoretically there are countless different types of beans, users can add new bean types to meet the actual application requirements according to their own needs.
1, singleton scope (scope default)
When a bean's scope 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, return only the same instance of the bean. In other words, when a bean definition is set to the singleton scope, the Spring IOC container creates only the only instance of the bean definition. This single instance is stored in the singleton cache (singleton cache), and all subsequent requests and references for that Bean will return the cached object instance, noting that the singleton scope and the singleton in the GOF design pattern are completely different. A single example design pattern means that only one class exists in a ClassLoader, and the singleton here means that a container corresponds to a bean, meaning that when a bean is identified as Singleton, Only one of the beans is present in the spring's IOC container.
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
Prototype scoped beans, each request (injected into another bean, or the Getbean () method of the container in a program way) produces a new instance of the bean, which is equivalent to the operation of the It is important for prototype scoped beans that spring cannot be responsible for the entire lifecycle of a prototype bean, which is delivered to the client after it has been initialized, configured, decorated, or assembled with a prototype instance. The prototype instance was then ignored. Regardless of the scope, the container invokes the initialization lifecycle callback method for all objects, and for prototype, any configured destructor lifecycle callback method will not be invoked. It is the responsibility of client code to clear the object of the prototype scope and release any expensive resources held by any prototype bean. (One possible way for the spring container to release resources from the singleton scope Bean is to hold a reference to the bean that is being purged by using the bean's back processor.) )
Configuration instance:
<bean id= "Role" class= "Spring.chapter2.maryGame.Role" scope= "prototype"/>
Or
<beanid= "Role" class= "Spring.chapter2.maryGame.Role" singleton= "false"/>
3. Request
The request indicates that a new bean will be generated for each HTTP request, and that the bean is valid only within the current HTTP requests, configuring the instance:
When the request, session, and global session are used, the following configuration is first done in the web.xml of the initialization web:
If you are using a Web container of servlet 2.4 and above, you only need to add the following contextlistener to 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 the previous Web container was Servlet2.4, you would 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>

The scope of the bean can then be configured:
<bean id= "role" class= "Spring.chapter2.maryGame.Role" scope= "request"/>
4, The session
Sessions scope indicates that each HTTP request will produce a new bean and that the bean is valid only within the current HTTP session, configuring instance:
Configuration instance:
and the prerequisites for the request configuration instance, Configure the Web boot file can be configured as follows:
<bean id= "role" class= "Spring.chapter2.maryGame.Role" scope= "session"/>
5, Global The session
Global session scope is similar to the standard HTTP session scope, but it only makes sense in portlet-based Web applications. The Portlet specification defines the concept of a global session, which is shared by all of the different portlets that make up a portlet Web application. The bean defined in the global session scope is limited to the lifecycle scope of the globally portlet session. If you use the global session scope in the Web to identify the bean, the Web is automatically used as the type of sessions.
Configuration instance:
as well as the prerequisites for the request configuration instance, configuring the Web boot file can be configured as follows:
<bean id= "role" class= "Spring.chapter2.maryGame.Role" Scope= "Global Session"/>
6, custom bean assembly scopes
Scopes can be arbitrarily extended in spring2.0, you can customize scopes, and even you can redefine existing scopes (but you can't overwrite Singleton and prototype), The scope of spring is defined by the interface Org.springframework.beans.factory.config.Scope, as long as the interface is implemented by customizing its own scope, here's an example:
We create a scope for a thread, The scope is valid in the representation of a thread, and 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 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;
}
}

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.