1. Introduction
When a Spring bean is defined, it is actually a recipe for creating a class instance, which means that many objects of that class can be created with this recipe. 5 scopes supported by the Spring framework:
2. Introduction to a single case scope
The singleton scope is the default scope, and the singleton Bean only produces an instance object of the type that the bean corresponds to, and for all requests, the spring container returns only one instance. This instance is stored in the spring container's singleton pool cache, after which all requests and references to the Bean object are taken from the singleton cache pool instead of being regenerated into a new object.
However, the point to note is that the singleton cache is not a narrow class loader-level singleton concept that we previously learned in design mode. A singleton in design mode refers to an instance of a singleton class that is generated in every classloader. But for spring, the Singleton is that only one instance of the class will be generated within the entire spring container's life span. In spring, the bean default scope is a singleton scope. For example:
3.prototype Prototyping Scope
When a bean is defined as prototype, a new instance of the class is returned for each request, that is, the prototype bean is injected into another bean, or the request prototype bean is generated by invoking the Getbean () method on the container. In general, the prototype bean is used for stateful beans, and singleton beans are used for stateless beans. In fact, for a prototype bean, the spring container simply replaces the new operator, and all life-cycle management of the resulting bean object will be processed by the client after the spring instantiation. Spring does not manage the complete life cycle of any prototype bean, so client code must clean up the prototype object and manually release the resources held by the prototype Bean. In order for the spring container to release the resources held by the prototype bean, it can use a custom Bean[post-processor], which holds a reference to the bean that needs to be cleaned up.
Break the net, the remaining three next to fill up.
Reference Source: Https://github.com/b2gats/stone-docs/blob/master/spring-4-beans.md#beans-factory-scopes-prototype
Scope of Spring Bean