DEV Community

Saravanan B
Saravanan B

Posted on

Spring 5 - Bean Scope

A bean scope refers to the lifecycle of a bean. How long the bean lives. How many instances need to be created.

Default scope is singleton.
Spring container creates one instance of bean shared across.

Image description

Image description

code example

Singleton
Image description

Image description

Prototype

Image description

Image description

Bean Lifecycle:

Spring Container starts beans are instantiated -> dependency Injected -> Internal spring processing -> custom init method -> Bean ready to use -> Application Shut down (custom method to close bean is executed before application shutdown).

Top comments (0)