Spring Source Analysis (13) Get a singleton bean in the cache

Source: Internet
Author: User

Abstract: This article combines the spring source depth analysis to analyze the source code of Spring 5.0.6 version. If there is any description of the error, please correct me.

After introducing the usage of Factorybean, we can understand the process of bean loading. As mentioned earlier, the singleton is only created once in the same container in spring, and the subsequent fetch bean is fetched directly from the singleton cache, and of course it is just trying to load, first trying to load from the cache and then trying to load from the singletonfactories. Because there is a dependency scenario when creating a singleton bean, and in order to avoid circular dependencies when creating dependencies, the principle of spring's creation of beans is that the objectfactory of the created bean will be added to the cache before the bean creation is complete. Once the next bean is created to rely on the last bean, use objectfactory directly.

@Override @nullable PublicObject Getsingleton (String beanname) {//parameter True sets the identity to allow early dependency    returnGetsingleton (Beanname,true);}/*** Return the (RAW) Singleton object registered under the given name. * <p>checks already instantiated Singleton S and also allows for the early * reference to a currently created singleton (resolving a circular reference). * @paramBeanname The name of the bean to look for *@paramAllowearlyreference Whether early references should be created or not *@returnthe registered singleton object, or {@codenull} If none found*/@NullableprotectedObject Getsingleton (String beanname,Booleanallowearlyreference) {    //whether an instance exists in the check cacheObject Singletonobject = This. Singletonobjects.get (Beanname); if(Singletonobject = =NULL&&issingletoncurrentlyincreation (Beanname)) {        //if empty, the global variable is locked and processed        synchronized( This. Singletonobjects) {            //If this bean is being loaded, it is not processedSingletonobject = This. Earlysingletonobjects.get (Beanname); if(Singletonobject = =NULL&&allowearlyreference) {                //when some methods require early initialization, the Addsingletonfactory method is called to store the corresponding objectfactory initialization path in SingletonfactoriesObjectfactory<?> singletonfactory = This. Singletonfactories.get (Beanname); if(Singletonfactory! =NULL) {                    //call a pre-set GetObject methodSingletonobject =Singletonfactory.getobject (); //record in cache, earlysingletonobjects and singletonfactories mutually exclusive                     This. Earlysingletonobjects.put (Beanname, singletonobject);  This. Singletonfactories.remove (Beanname); }            }        }    }    returnSingletonobject;}

This approach is confusing for many readers because of the detection of cyclic dependencies and the access to records that involve many variables. This method first attempts to obtain an instance from the singletonobjects surface, if it is not obtained again from the earlysingletonobjects, if it is not obtained, Try to get beanname corresponding objectfactory from singletonfactories, then call this objectfactory GetObject to create the bean and put it in Earlysingletonobjects inside, and remove the objectfactory from the singletonfacotories, and all subsequent memory operations are only used for cyclic dependency detection, That is, it is only used if Allowearlyreference is true.
This fling involves a different map for storing beans, which may cause the reader to feel a crash, simply explained below.

Singletonobjects The relationship between saving beanname and creating bean instances, bean name---bean instance
Singletonfactories The relationship between the factory that holds the beanname and the bean creation, bean name---Objectfactory
Earlysingletonobjects is also the relationship between saving beanname and creating bean instances, unlike singletonobjects, when a singleton bean is placed in this face, then when the bean is still in the process of being created, it can be obtained by means of the Getbean method, Its purpose is to detect cyclic references
Registeredsingletons Used to save all the currently registered beans

Spring Source Analysis (13) Get a singleton bean in the cache

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.