Six scopes of Bean

Table of contents

1. Bean scope definition

2. Bean scope classification

3. Effects and usage scenarios of the six scopes

3.1 singleton

3.2 prototype

3.3 request

3.4 session

3.5 application

3.6 websocket


1. Bean scope definition

The scope of a Bean refers to a certain behavior mode of a Bean in the entire framework of Spring. For example, the singleton scope means that there is only one copy of a Bean in the entire Spring. After one person modifies it, others can read it. The value taken is also the modified value.


2. Bean scope classification

Spring has a total of six scopes, the last four of which are based on Spring MVC

1. singleton: singleton scope

2. prototype: prototype scope (multiple instance scope)

3. request: request scope

4. session: session scope

5. application: global scope

6. websocket: HTTP WebSocket scope


3. Effects and usage scenarios of the six scopes

3.1 singleton

Singleton is the scope selected by Spring by default

There is only one instance of the Bean under this scope in the IOC container, and the usage scenario is usually when the property state of the Bean object does not need to be updated.


3.2 prototype

Prototype means that a new instance will be created every time a bean request under the scope is requested, that is, when we get a bean or assemble a bean, a new object instance will be created

Scenario: When the property status of the Bean object often needs to be updated.


3.3 request

request means that a new Bean instance will be created every time an http request is made, similar to prototype, which is generally applied to a shared bean for an http request and response 


3.4 session

Session generally defines a Bean instance in an http session, which is generally used in shared beans for user sessions , such as when recording a user's login information

 


3.5 application

application defines a Bean instance in an http servlet context. The application scenario is generally the context information of a web application , such as recording the shared information of an application


3.6 websocket

Websocket refers to defining a Bean instance in the life cycle of an HTTP WebSocket; when applied in a WebSocket session, a Map structure header information is saved, which will be used to wrap the client information header.

Guess you like

Origin blog.csdn.net/yss233333/article/details/129635989