On the scope and life cycle of bean in spring

This paper mainly explores the scope and life cycle of beans, as follows.

Scope of bean

Spring 3 defines five scopes for beans, namely singleton, prototype, request, session and global session. The descriptions of the five scopes are as follows:

1. Singleton: Singleton mode. Only one shared bean instance exists in the spring IOC container. No matter how many beans reference it, it always points to the same object. Singleton scope is the default scope in spring. You can also define beans as singleton mode and configure them as follows:

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, while the singleton global has only one object. As a rule of thumb, use the prototype scope for stateful beans and the 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 in 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 valid in the current HTTP request, while other requests cannot see the change of state in 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 in the current session.

< bean id = "userpreference" class = "com. IOC. Userpreference" scope = "session" / >, which is the same as the HTTP request. Each session request creates a new instance, but different instances do not share attributes, and the instance is only valid in their own session request. If 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 portlet context.

Bean life cycle

After the introduction of bean scope above, we will explain the life cycle of bean on the basis of bean scope.

The spring container can manage the life cycle of beans under the singleton scope. Under this scope, spring can accurately know when beans are created, initialized, and destroyed. For beans with prototype scope, spring is only responsible for creating. 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 the life cycle of those beans configured as prototype scope. The execution of bean life cycle in spring is a very complex process. Readers can use the methods provided by spring to customize the bean creation process. The spring container does a lot of work before ensuring that a bean instance can be used:

summary

The above is all about the scope and life cycle of beans in spring. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>