Bean's scope and life cycle

Source: Internet
Author: User
Tags http request valid

One, the spring framework supports five scopes (three of which are available only in the Web-based Spring ApplicationContext).
The scope of the built-in support is as follows: Scope description

Singleton

A bean definition corresponds to an object instance in each spring IOC container.

Prototype

A bean definition corresponds to multiple object instances.

Request

In an HTTP request, a bean definition corresponds to an instance, that is, each HTTP request will have its own bean instance, which is created from a bean definition. This scope is valid only in the case of a web-based spring ApplicationContext.

Session

In an HTTP Session, a bean definition corresponds to an instance. The scope is valid only in the case of web-based springapplicationcontext.

Global session

In a global HTTP Session, a bean definition corresponds to an instance. Typically, this is only valid when using the Portlet context. This scope is valid only in the case of a web-based spring ApplicationContext.

1.Singleton Scopes

When a bean is scoped to Singleton, there will only be one shared bean instance in the spring IOC container, and all requests to the bean will only return the same instance of the bean as long as the ID matches that bean definition. In other words, when a bean definition is set to Singlton 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.

  Note that spring's singleton bean concept is completely different from the singleton pattern defined in the GOF schema book. The so-called object scope in the classic Gof singleton mode refers to the instance that is created in each of the classloader    specified class and has only one  . It is most appropriate to describe spring's singleton scope as a container  (container) corresponding to a bean  instance. That is, if a bean of a specified class is defined within a single spring container, then the spring container will create one and only one   instance of the class specified by the bean definition. The singleton scope is the default scope   in spring. To define a bean as singleton in XML, you can configure it like this: XML code    <bean id= "Accountservice"  class= " Com.foo.DefaultAccountService "/>      <!-- the following is  equivalent, though redundant  (Singleton scope is the default);  using  spring-beans-2.0.dtd  or upper-->   <bean id= "Accountservice"   class= "Com.foo.DefaultAccountService"  scope= "singleton"/>      <!-- the  following is equivalent and preserved for backward compatibility  in spring-beans.dtd -->   <bean id= "Accountservice"  class= "Com.foo.DefaultAccountService"  singleton= "true"/>     

Test:

The Beans.xml configuration file is configured as follows: XML code <bean id= "Personservice" class= "Examples.test.PersonServiceBean" ></bean>

The test code looks like this: Java code public class Springtest {@Test public void instancespring () {ApplicationContext CTX           = new Classpathxmlapplicationcontext ("Beans.xml");           Personservice PersonService1 = (personservice) ctx.getbean ("Personservice");           Personservice PersonService2 = (personservice) ctx.getbean ("Personservice");       System.out.println (PERSONSERVICE1==PERSONSERVICE2); }   }

Run the test program and the result output is: true . The description is that only one personservicebean is created.

2. Prototype Scope

A prototype-scoped bean causes a new bean instance to be created each time the bean request is injected into another bean, or the Getbean () method of the container is called programmatically. Based on experience, you should use the prototype scope for stateful beans, and the singleton scope for stateless beans.

The following illustration shows the prototype scope of spring. Note that in general, DAO is not configured to prototype because DAO usually does not hold any session state, so you should use singleton scope.


The bean is defined as prototype in XML, which can be configured like this: XML code <!--using SPRING-BEANS-2.0.DTD or upper-<bean id= "Accountservice" C lass= "Com.foo.DefaultAccountService" scope= "prototype"/> <!--the following is equivalent and preserved for Backwa RD compatibility in Spring-beans.dtd-<bean id= "Accountservice" class= "Com.foo.DefaultAccountService" Singleton= "false"/>

Change the configuration information in the Beans.xml configuration file to resemble the following: XML code <bean id= "Personservice" class= "Cn.itcast.service.impl.PersonServiceBean" Scope= "Prototype" ></bean>

Then run the above test program, the output is: false , indicating the creation of two Personservicebean.

Other scopes, request, session, and global sessions are only used in Web-based applications (don't care about what Web application framework you're using). These scopes are only useful when using a web-based spring ApplicationContext implementation, such as Xmlwebapplicationcontext. If you are trying to use these scopes in a normal spring IOC container, such as Xmlbeanfactory or Classpathxmlapplicationcontext, You will get a IllegalStateException exception (unknown bean scope).

Second, the Bean's life cycle

(1) When to initialize an instance of a bean

When Scope=singleton, which is the default, is instantiated when the container is initialized. But we can specify the bean node's lazy-init= "true" to defer initialization of the bean, when the bean is initialized only the first time the bean is requested, that is, when the bean is asked for the first time. As shown in the following configuration: XML code

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.