Spring Bean Scopes Explained With Code Samples
The Spring Framework allows defining scopes for a Spring Bean. Spring Bean scopes define the lifetime of a Spring bean. In this article, we will take a look at bean scopes. What is Spring Bean Scope? A Spring Bean scope specifies the lifetime of a Spring bean. So it specifies when the bean will be created and how long it will exist in the system. Edit What are the different bean scopes? Spring supports the following bean scopes: Singleton This is the default bean scope. As per this scope, only one instance of a bean is created. Each time the bean is requested, the same instance is returned. Prototype As per this scope, a new instance of a bean is created each time the bean is requested. Request This bean scope is valid only for a web application. As per this bean scope, a new instance of the bean is created for each HTTP request. The bean stays alive until the HTTP request completes. If you use this bean scope for a standalone application, your code will result in an...