Scope refers to the life cycle of the bean.
How long does Bean live?
How many instances are created?
How is the bean shared?
The default scope of bean is singleton.
What is a singleton?
Spring container creates only one instance of the bean, by default
It is cached in memory.
All dependency injection for the bean will point to the same bean
Additional spring bean scope
Scope | Description |
singleton | Create a single shared instance of the bean. Default scope. |
prototype | create a new bean instance for each container request. |
request | Scoped to an HTTP web request, only used for web apps. |
session | Scoped to an HTTP web session, only used for web apps. |
application | scoped to a web app servlet context. only used for web apps. |
WebSocket | scoped to a web socket only used for web apps. |
Bean Lifecycle Methods
Prototype Beans and Lazy Initialization
Prototype beans are lazy by default. There is no need to use the @Lazy annotation for prototype scope beans.