How to Use spring's scope: <bean id = "role" class = "Spring. chapter2.marygame. role "Scope =" Singleton "/> here, 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 are made, if the ID matches the bean definition, only the same instance of the bean 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. For the bean deployed in the prototypeprototype scope, each request (inject it into another bean, or call the container's
getBean()Method) will generate a new bean instance, which is equivalent to a new operation. It is very important for beans with prototype scope, that is, spring cannot be 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, 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. The bean is valid only in the current HTTP request, configuration 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: <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 the implementation of javax. servlet. filter: <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. Sessionsession scope indicates that a new bean is generated for each HTTP request, and the bean is valid only in the current HTTP session, configuration instance: configure the instance: The same as the prerequisite for configuring the instance for request, you can configure the Web startup file as follows: <bean id = "role" class = "Spring. chapter2.marygame. role "Scope =" session "/> 5. The Global sessionglobal 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. Configure an 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. The scope of custom bean assembly can be expanded as needed in spring2.0. 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. config. define scope. to customize your own scope, you only need to implement this interface. For example, we will 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;
}
}