Spring (3.2.3), spring3.2.3
Spring supports five scopes: singleton, prototype, request, session, and global session.
| Scope |
Description |
| Singleton |
(Default scope) singleton mode. Each Spring IoC container will instantiate only one Bean defined by singleton. |
| Prototype |
Prototype, |
| Request |
For each HTTP request, the Bean defined by the request generates a new instance. This scope is valid only when Spring is used in Web applications. |
| Session |
For each HTTP session, a new instance is generated using the Bean defined by the session. This scope is valid only when Spring is used in Web applications. |
| Global session |
This scope is valid only when Spring is used in Web applications. |
Singleton Scope
By default, after the Spring IoC container is started, all instances of beans defined by singleton are created and configured in singleton mode during initialization. For each configured Bean, the entire Spring container shares a unique instance. Containers track the status of Bean instances and maintain the lifecycle of Bean instances. Each time the container obtains the Bean defined by singleton, the container returns the same instance.
Prototype Scope
During the initialization of the Spring IoC container, instances of beans defined by prototype are not created. The container is not responsible for tracking the status of the Bean instance, nor for maintaining the lifecycle of the Bean instance. Each time you obtain an instance of the Bean defined by prototype through the container, the container creates a new instance and returns the result.