Spring Source Analysis: Spring life Cycle II

Source: Internet
Author: User

Next, we'll look at where the code is executed.

1. Initialize Beanfactorypostprocessor

Invokebeanfactorypostprocessors (beanfactory);-->

Postprocessorregistrationdelegate.invokebeanfactorypostprocessors (Beanfactory, GetBeanFactoryPostProcessors ());


list<beanfactorypostprocessor> nonorderedpostprocessors = new arraylist<> ();
        for (String postprocessorname:nonorderedpostprocessornames) {
    // There is a getbean here for the Beanfactorypostprocessor initialization operation    
    Nonorderedpostprocessors.add (Beanfactory.getbean ( Postprocessorname, Beanfactorypostprocessor.class));
        }

Here perform beanfactorypostprocessor.postprocessbeanfactory
    invokebeanfactorypostprocessors ( Nonorderedpostprocessors, beanfactory);

Executive Beandefinitionregistrypostprocessor.postprocessbeandefinitionregistry

    private static void Invokebeandefinitionregistrypostprocessors (
            collection<? extends Beandefinitionregistrypostprocessor> postprocessors, Beandefinitionregistry Registry) {for

        ( Beandefinitionregistrypostprocessor postprocessor:postprocessors) {
            Postprocessor.postprocessbeandefinitionregistry (registry);
        }
    }

2. Execution of Beanfactorypostprocessor's postprocessbeanfactory

private static void Invokebeanfactorypostprocessors (
            collection<? extends beanfactorypostprocessor> Postprocessors, Configurablelistablebeanfactory beanfactory) {for

        (beanfactorypostprocessor postProcessor: postprocessors) {
            postprocessor.postprocessbeanfactory (beanfactory);
        }
    }

3. Initialize the beanpostprocessor.

registerbeanpostprocessors (beanfactory);--> protected void registerbeanpostprocessors (Configurablelistablebeanfactory beanfactory)
    {postprocessorregistrationdelegate.registerbeanpostprocessors (beanfactory, this); } public static void Registerbeanpostprocessors (Configurablelistablebeanfactory beanfactory, Abstractap
        Plicationcontext applicationcontext) {....//Now, register all regular beanpostprocessors.
        list<beanpostprocessor> nonorderedpostprocessors = new arraylist<> (); for (String ppname:nonorderedpostprocessornames) {//Getbean beanpostprocessor is also instantiated here Beanpostproc
            Essor pp = Beanfactory.getbean (ppname, Beanpostprocessor.class);
            Nonorderedpostprocessors.add (PP);
            if (pp instanceof mergedbeandefinitionpostprocessor) {internalpostprocessors.add (PP); }
        }
....
}

4. Execute instantiationawarebeanpostprocessor.postprocessbeforeinstantiation
Here is a singleton example

Abstractbeanfactory.dogetbean (final String name, @Nullable final class<t> Requiredtype, @Nullable final
                Object[] args, Boolean typecheckonly) throws Beansexception {...//Create bean instance. if (Mbd.issingleton ()) {sharedinstance = Getsingleton (Beanname, (), {TR
                        Y {return Createbean (Beanname, mbd, args); } catch (Beansexception ex) {//explicitly remove instance from Singl Eton Cache:it might has been put there//eagerly by the creation process, to allow for CIRC
                            Ular Reference resolution.
                            Also Remove any beans this received a temporary reference to the bean.
                            Destroysingleton (Beanname);
                        Throw ex;
}
                    });

...
} Enter Createbean Object Bean= Resolvebeforeinstantiation (Beanname, mbdtouse);
        Protected Object Resolvebeforeinstantiation (String beanname, rootbeandefinition mbd) {object bean = null; if (! Boolean.FALSE.equals (mbd.beforeinstantiationresolved)) {//Make sure Bean class was actually resolved at this
            Point.  if (!mbd.issynthetic () && hasinstantiationawarebeanpostprocessors ()) {class<?> TargetType =
                Determinetargettype (Beanname, mbd);
                    if (targetType! = null) {bean = Applybeanpostprocessorsbeforeinstantiation (TargetType, beanname); if (bean! = null) {bean = Applybeanpostprocessorsafterinitialization (bean, bean
                    Name);
        }}} mbd.beforeinstantiationresolved = (bean! = null);
    } return bean; } protected Object applybeanpostprocessorsbeforeinstantiation (class<?> beanclass, String BeannaMe) {for (Beanpostprocessor bp:getbeanpostprocessors ()) {if (BP instanceof INSTANTIATIONAWAREBEANP
                Ostprocessor) {Instantiationawarebeanpostprocessor IBP = (instantiationawarebeanpostprocessor) BP;
                Object result = Ibp.postprocessbeforeinstantiation (Beanclass, beanname);
                if (result! = NULL) {return result;
    }}} return null;
 The above has been executed postprocessbeforeinstantiation

5. Executing the Bean's constructor

Enter Object beaninstance = Docreatebean (Beanname, Mbdtouse, args);


if (Instancewrapper = = null) {
            //This is initialized
            Instancewrapper = Createbeaninstance (Beanname, mbd, args);
        }

6. Implementation
Postprocessbeforeinstantiation.postprocesspropertyvalues

Enter Populatebean (Beanname, MBD, instancewrapper);



if (Hasinstawarebpps | | needsdepcheck) {
            propertydescriptor[] Filteredpds = Filterpropertydescriptorsfordependencycheck (BW, mbd.allowcaching);
            if (Hasinstawarebpps) {for
                (Beanpostprocessor bp:getbeanpostprocessors ()) {
                    if (BP instanceof Instantiationawarebeanpostprocessor) {

//execute postprocessbeforeinstantiation.postprocesspropertyvalues

                        here PVs = Ibp.postprocesspropertyvalues (PVs, Filteredpds, Bw.getwrappedinstance (), beanname);
                        if (PVs = = null) {
                            return;}}}
            }
            if (Needsdepcheck) {
                checkdependencies (beanname, MBD, Filteredpds, PVS);
            }
        }


Applypropertyvalues (Beanname, MBD, BW, PVS);

7. Inject attributes into the bean

Applypropertyvalues (Beanname, MBD, BW, PVS);

8. Implementing the bean implementation of the relevant aware interface

Enter  Exposedobject = Initializebean (Beanname, Exposedobject, mbd);
This gives aware the ability to give aware interface
invokeawaremethods (beanname, Bean);

9. Executive Beanpostprocessor.postprocessbeforeinitialization

if (mbd = = NULL | |!mbd.issynthetic ()) {
            Wrappedbean = applybeanpostprocessorsbeforeinitialization (Wrappedbean, beanname);
        }



    public Object Applybeanpostprocessorsbeforeinitialization (object Existingbean, String beanname)
            throws beansexception {

        Object result = Existingbean;
        For (Beanpostprocessor beanprocessor:getbeanpostprocessors ()) {
            Object current = Beanprocessor.postprocessbeforeinitialization (result, beanname);
            if (current = = null) {
                return result;
            }
            result = current;
        }
        return result;
    }

10. Execute @postconstrut Initialization method

The execution commonannotationbeanpostprocessor.postprocessbeforeinitialization specifically why the method is called in the following unified explanation

11. Execute Initializingbean.

Enter Invokeinitmethods (Beanname, Wrappedbean, mbd); protected void Invokeinitmethods (String beanname, Final Object Bean, @Nullable rootbeandefinition mbd) throws
        Throwable {Boolean Isinitializingbean = (bean instanceof Initializingbean);
            if (Isinitializingbean && (mbd = = NULL | |!mbd.isexternallymanagedinitmethod ("afterpropertiesset"))) { if (logger.isdebugenabled ()) {Logger.debug ("invoking Afterpropertiesset () on beans with name '" + Beannam
            E + "'"); } if (System.getsecuritymanager () = null) {try {Accesscontroller.dopriv Ileged ((privilegedexceptionaction<object>) (), {((Initializingbean) bean). Afterpropert
                        Iesset ();
                    return null;
                }, Getaccesscontrolcontext ());
      } catch (Privilegedactionexception pae) {throw pae.getexception ();          }} else {//called here ((Initializingbean) bean). afterpropertiess
            ET ();
            }} if (mbd! = null) {String initmethodname = Mbd.getinitmethodname (); if (Stringutils.haslength (initmethodname) &&! ( Isinitializingbean && "Afterpropertiesset". Equals (Initmethodname)) &&!mbd.isexternall
            Ymanagedinitmethod (Initmethodname)) {Invokecustominitmethod (Beanname, Bean, mbd); }
        }
    }

12. Execution of XML Init-method

if (mbd! = null) {
            String initmethodname = Mbd.getinitmethodname ();
            if (Stringutils.haslength (initmethodname) &&
                    ! ( Isinitializingbean && "Afterpropertiesset". Equals (Initmethodname) &&
                    ! Mbd.isexternallymanagedinitmethod (Initmethodname)) {
                Invokecustominitmethod (beanname, Bean, mbd);
            }
        }

13. Executive Beanpostprocessor.postprocessafterinitialization

if (mbd = = NULL | |!mbd.issynthetic ()) {
            Wrappedbean = applybeanpostprocessorsafterinitialization (Wrappedbean, beanname);
        }
@Override Public
    Object Applybeanpostprocessorsafterinitialization (Object Existingbean, String beanname)
            Throws Beansexception {

        Object result = Existingbean;
        For (Beanpostprocessor beanprocessor:getbeanpostprocessors ()) {
            Object current = Beanprocessor.postprocessafterinitialization (result, beanname);
            if (current = = null) {
                return result;
            }
            result = current;
        }
        return result;
    }

The code above is Abstractapplicationcontext. Refresh and Abstractbeanfactory.dogetbean can be found at the entrance step by step.

The spring life cycle is explained in the previous article, which explains where the spring is implemented. The next article explains the application and how annotations are implemented, such as @configutation, @Import, @PostConstrut, and so on.

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.