Scope of Spring Bean

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.