This article mainly explores related content about the scope and life cycle of beans, as follows.
Bean's scope
Spring 3 defines five scopes for Bean, namely singleton (singleton), prototype (prototype), request, session and global session. The five scopes are explained as follows:
1.singleton: Singleton mode, there will only be one shared bean instance in the Spring IoC container, no matter how many beans refer to it, they always point to the same object. Singleton scope is the default scope in Spring. You can also define beans as singleton mode and configure them as:
<bean id="userDao" scope="singleton"/>
2.prototype: Prototype mode. Every time a prototype defined bean is obtained through a Spring container, the container will create a new bean instance. Each bean instance has its own attributes and state, and singleton has only one object in the global context. 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. However, a new bean will be generated for different Http requests, and the bean is only valid within the current Http Request.
<bean id="loginAction" scope="request"/> , for each Http request, the Spring container creates a brand new instance according to the definition of the bean, and the instance is only valid within the current Http request, and other requests cannot see the change in the status in the current request. When the current Http request ends, the bean instance will also be destroyed.
4.session: In a 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" scope="session"/> , the same as Http request, create a new instance every session request, and no attributes are shared between different instances, and the instance is only valid within its 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 the portlet context.
The life cycle of a bean
After the introduction of the scope of beans as mentioned above, the life cycle of beans will be explained based on the scope of beans.
Spring containers can manage the life cycle of beans under singleton scope. In this scope, Spring can know exactly when the bean is created, when the initialization is completed, and when it is destroyed. For prototype scope beans, 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 the life cycle of those beans configured as prototype scope. The execution of the life cycle of a bean in Spring is a very complex process, and readers can use the methods provided by Spring to customize the bean creation process. Spring containers do a lot of work before ensuring that a bean instance can be used:
Summarize
The above is all about this article discussing the scope and life cycle of beans in Spring. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!