Bean Scope
Explore the concept of bean scope within the Spring Framework to understand how bean lifecycle and instance management works. This lesson focuses on singleton and prototype scopes, explaining their differences through practical examples including how Spring manages instances in an application context and how it contrasts with traditional singleton design patterns.
We'll cover the following...
The Spring container manages beans. The term bean scope refers to the lifecycle and the visibility of beans. It tells how long the bean lives, how many instances of the bean are created, and how the bean is shared.
Types of bean scopes
There are six types of scopes: singleton, prototype, request, session, application, and websocket.
The singleton and prototype scopes can be used in any application while the last four scopes are only available for a web application. In this lesson, we will focus on singleton and prototype bean scopes only.
Singleton scope
The default scope of a bean is singleton, in which only one instance of the bean is created and cached in memory. Multiple requests for the bean return a shared reference to the same bean. In contrast, prototype scope ...