Scope and life cycle of beans in Spring

  • Bean scope

   Spring 3 defines five scopes for beans, namely singleton (singleton), prototype (prototype), request, session and global session. The five scopes are described as follows:

  1. singleton: singleton mode, there is only one shared bean instance in the Spring IoC container, no matter how many beans reference it, it always points to the same object. The Singleton scope is the default scope in Spring. You can also explicitly define a Bean as a singleton mode, and configure it as:
    • <bean id="userDao" class="com.ioc.UserDaoImpl" scope="singleton"/>
  2. Prototype: prototype mode, every time the bean defined by the prototype is obtained through the Spring container, the container will create a new bean instance, each bean instance has its own properties and state, and the singleton has only one global object. As a rule of thumb, use prototype scope for stateful beans and singleton scope for stateless beans.
  3. request: In an Http request, the container will return the same instance of the bean. For different Http requests, a new bean will be generated, and the bean is only valid within the current Http Request.
    • <bean id="loginAction" class="com.cnblogs.Login" scope="request"/>, for each Http request, the Spring container creates a new instance according to the definition of the bean, and the instance is only used in the current Http It is valid within the request, and other requests cannot see the changes in the state of the current request. When the current Http request ends, the bean instance will also be destroyed.
  4. session: In an Http Session, the container will return the same instance of the bean. For different Session requests, a new instance will be created, and the bean instance is only valid within the current Session.
    • <bean id="userPreference" class="com.ioc.UserPreference" scope="session"/>, the same as Http request, each session request creates a new instance, and different instances do not share attributes, and the instance It is only valid within its own session request. When the request ends, the instance will be destroyed.
  5. global Session: In a global Http Session, the container will return the same instance of the bean, which is only valid when using the portlet context.
  • Bean life cycle

  After the introduction of the bean scope as above, the life cycle of the bean will be explained on the basis of the bean scope.

  The Spring container can manage the life cycle of beans in the singleton scope. In this scope, Spring can know exactly when a bean is created, initialized, and destroyed. For beans with prototype scope, Spring is only responsible for creating them. When the container creates an instance of the bean, the instance of the bean is handed over to the client's code management. The Spring container will no longer track its life cycle and will not manage those The life cycle of beans configured as prototype scope. The execution of the Bean life cycle in Spring is a very complicated process, and readers can use the methods provided by Spring to customize the Bean creation process. The Spring container does a lot of work before making sure a bean instance is available:

 

                   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325700673&siteId=291194637