Spring Re-contact scope range

Source: Internet
Author: User

<bean id= "UserService" class= "Com.bjsxt.service.UserService" scope= "prototype" >
<!--
<property name= "Userdao" ref= "U"/>
-
<constructor-arg>
<ref bean= "U"/>
</constructor-arg>
</bean>

The value of the Bean's score

1. Singleton scope (scope default)

When the scope of a bean is set to Singleton, there will only be one shared bean instance in the spring IOC container, and all requests to the bean As long as 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 singleton scope, the Spring IOC container only creates a unique instance of the bean definition. This single instance is stored in the singleton cache (singleton cache), and all subsequent requests and references to the bean will return the cached object instance, noting that the singleton in the singleton scope and GOF design pattern is completely different, The singleton design pattern indicates that only one class exists in a ClassLoader, and here the singleton represents a container for a bean, which means that when a bean is identified as Singleton, Only one of the beans will exist in spring's IOC container.

Configuration example:<id=class=scope="singleton"/> or <id=class= singleton= "true"/>           
2. Prototype

prototype scope deployed beans, each request (inject it into another bean, or programmatically invoke the container's Getbean () method) produces a new instance of the bean, which is quite important for the prototype-scoped bean, which is that spring cannot be responsible for the entire life cycle of a prototype bean, the container is initialized, CONFIGURED, Decorate or assemble a prototype instance, hand it over to the client, and then leave the prototype instance unattended. Regardless of the scope, the container invokes the initialization lifecycle callback method for all objects, and for prototype, any configured destructor life-cycle callback methods will not be called. Clearing prototype-scoped objects and releasing expensive resources held by any prototype bean is the responsibility of the client code. (One possible way for the spring container to release the resource that is consumed by the singleton scope Bean is by using the bean's post processor, which holds a reference to the bean to be purged.) )

Configuration example:<id=class=scope="prototype"/> or <beanid=class=  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, configuration instance: request, session, global session when using the first time in the initialization of Web. XML in the following configuration: If you are using a Web container that is servlet 2.4 and above, you only need to add the following contextlistener to the Web application's XML declaration file, the

<Web-app> ...  <listener><listener-class> Org.springframework.web.context.request.RequestContextListener</listener-class</  Listener> ... </web-app>               

If you are Servlet2.4 a previous web container, then 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< Span class= "Cm-tag cm-bracket" >>   <filter-name< Span class= "Cm-tag cm-bracket" >>requestcontextfilter</ filter-name>   < url-pattern>/*</< Span class= "Cm-tag" >url-pattern> </filter-mapping>   ... </web-app>   

You can then configure the scope of the bean:

<id=class=scope="request"/>    
4. Session

The session scope indicates that a new bean is generated for each HTTP request, and that the bean is valid only within the current HTTP session, and the configuration instance: Configuration instance: As with the request configuration instance, Configuring a Web Startup file can be configured as follows:

<id=class=scope="session"/>    
5. Global session

The 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 the various portlets that make up a portlet Web application. The bean defined in the global session scope is limited to the lifetime of the world Portlet session. If you use the global session scope to identify the bean in the Web, the Web is automatically used as the session type. Configuration instance: As with the request configuration instance, the configuration of the Web boot file can be configured as follows:

<id=class=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 you implement the interface, and here's an example: we create a scope for a thread, The scope is valid in representing a thread, and the code is as follows:

PublicclassMyScopeImplementsScope {PrivatefinalThreadLocalThreadscope=NewThreadLocal () {ProtectedObjectInitialValue () {ReturnnewHashMap (); }     }; ?PublicObjectGetStringNameObjectfactoryObjectfactory) {MapScope= (MAP)Threadscope.Get ();ObjectObject=Scope.Getname);IfObject==NULL) {Object=Objectfactory.GetObject ();Scope.PutNameobject); }ReturnObject }PublicObjectRemoveStringName) {MapScope= (Map) threadscope.        Get (); return scope.      Remove (name); } publicvoid registerdestructioncallback (String name, Runnable callback) {} PU     Blic String Getconversationid () { //TODO auto-generated method stub returnnull; } }

Spring re-contact scope range

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.