Bean Lifecycle: Prototype Scoped Beans
Learn about the complete lifecycle of prototype scoped beans and how it is managed.
We'll cover the following
Lifecycle of prototype beans
Spring manages the entire lifecycle of singleton beans but it does not completely manage the lifecycle of prototype beans. This is because there might be a large number of prototype instances and the container can become overwhelmed keeping track of them.
Note: The Spring container creates the prototype beans and hands them over when requested. Thereafter, it is the responsibility of the application to destroy the bean and free up any resources that it has acquired.
For the code example shown in this lesson, we will use the lesson11
sub-package created in the last lesson and add a class with prototype scope.
We will use the Movie
class from the “Bean Scope” lesson to show that the post-initialization method is called, but the pre-destruction method is not called for prototype beans. This class has a prototype scope as shown:
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.