Bean scopes in spring boot

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
singletonCreate a single shared instance of the bean. Default scope.
prototypecreate a new bean instance for each container request.
request Scoped to an HTTP web request, only used for web apps.
sessionScoped to an HTTP web session, only used for web apps.
applicationscoped to a web app servlet context. only used for web apps.
WebSocketscoped 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.

Author: Susheel kumar

Leave a Reply

Your email address will not be published. Required fields are marked *