Spring IOC Source Simple Analysis 04-bean initialization

Source: Internet
Author: User

# # Ready# # TargetLearn how Spring initializes bean instances# #测试代码 gordon.study.spring.ioc.IOC04_Initialization. Java public class Ioc04_initialization {     public static void main (string[] args) {        Resource Resource = new Classpathresource ("Ioc/ioc04.xml");        Defaultlistablebeanfactory Factory = new Defaultlistablebeanfactory ();        Beandefinitionreader reader = new Xmlbeandefinitio Nreader ((beandefinitionregistry) factory);        reader.loadbeandefinitions (Resource);        Factory.addbeanpostprocessor (new Innerbeanpostprocessor ());        Innerclass Inner = Factory.getbean ("inner", Innerclass.class);        SYSTEM.OUT.PRINTLN ("Level:" + Inner.level);        Factory.destroysingleton ("inner");   }     Static Class Innerbeanpostprocessor implements Beanpostprocessor {         @Override     & nbsp   Public Object Postprocessbeforeinitialization (object Bean, String beanname) throws Beansexception {            SYSTEM.OUT.PRINTLN ("in postproces Sbeforeinitialization ();            return bean;       } &NB Sp       @Override         public object Postprocessafterinitialization (object bean, Strin G beanname) throws Beansexception {            SYSTEM.OUT.PRINTLN ("in Postprocessafteriniti Alization ();            if (bean instanceof Innerclass) {        &N Bsp       ((innerclass) bean). Level = 3;           }            return bean;       }   }     Static class Innerclass Implemen TS Beannameaware, Beanclassloaderaware, Beanfactoryaware, Initializingbean, Disposablebean {         Private int Level;         public innerclass () {            SYSTEM.OUT.PRINTLN ("C Onstruct innerclass ... ");       }         @Override       &NBS P public void Setbeanname (String name) {            SYSTEM.OUT.PRINTLN ("in Setbeanname () ..." + Name);       }         @Override         public void set Beanclassloader (ClassLoader ClassLoader) {            SYSTEM.OUT.PRINTLN ("in Setbeanclassloader () ... "+ classLoader);       }         @Override   &N Bsp     public void setbeanfactory (Beanfactory beanfactory) throws Beansexception {        &NBSP ;   System.out.println ("in Setbeanfactory () ..." + beanfactory);       }         public void setLevel (inT level) {            SYSTEM.OUT.PRINTLN ("in SetLevel () ...");        &NB Sp   This.level = level;       }         @Override       &NB Sp public void Afterpropertiesset () throws Exception {            System.out.println (' in Afterp Ropertiesset ();            level = 2;       }     & nbsp   @Override         public void Destroy () throws Exception {          &NB Sp System.out.println ("in Destroy () ...");       }         public void init () {& nbsp           SYSTEM.OUT.PRINTLN ("in init () ...");       }     &NB Sp   public void exit () {            SYSTEM.OUT.PRINTLN ("in exit () ...");      }   }}   ioc04.xml
<beans ...><bean id= "inner" class= "Gordon.study.spring.ioc.ioc04_initialization$innerclass" init-method= "Init" destroy-method= "Exit" ><property name= "level" value= "1"/></bean></beans> Execution Resultsconstruct Innerclass ...In setLevel () ...In setbeanname () ... innerIn setbeanclassloader () [email protected]In setbeanfactory () ... org.s[email protected]2d8f65a4:defining beans [inner]; root of factory hierarchyIn postprocessbeforeinitialization () ...In afterpropertiesset () ...In init () ...In postprocessafterinitialization () ...Level:3In destroy () ...In exit () ...# # # # # # # # # Document Description Beanfactory class documents describe the extension points that are provided externally in the bean lifecycle. Bean factory implementations should support, the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their-order is:
  1. Beannameaware ' s Setbeanname
  2. Beanclassloaderaware ' s Setbeanclassloader
  3. Beanfactoryaware ' s Setbeanfactory
  4. Environmentaware ' s setenvironment
  5. Embeddedvalueresolveraware ' s Setembeddedvalueresolver
  6. Resourceloaderaware ' s Setresourceloader (only applicable if running in an application context)
  7. Applicationeventpublisheraware ' s Setapplicationeventpublisher (only applicable if running in an application context)
  8. Messagesourceaware ' s Setmessagesource (only applicable if running in an application context)
  9. Applicationcontextaware ' s Setapplicationcontext (only applicable if running in an application context)
  10. Servletcontextaware ' s Setservletcontext (only applicable if running in a Web application context)
  11. Postprocessbeforeinitialization Methods of Beanpostprocessors
  12. Initializingbean ' s Afterpropertiesset
  13. A custom Init-method definition
  14. Postprocessafterinitialization Methods of Beanpostprocessors
On shutdown of a bean factory, the following lifecycle methods apply:
    1. Postprocessbeforedestruction Methods of Destructionawarebeanpostprocessors
    2. Disposablebean ' s Destroy
    3. A custom Destroy-method definition
# # # example Code Analysis Innerclass is first instantiated by default constructor, outputconstruct Innerclass ...Then assemble the properties, callSetLevelmethod to set the Level property, outputIn setLevel () ...Then it starts initializing the bean. PassabstractautowirecapablebeanfactoryOfInitializebeanMethod. Then, the 1620th line of code deals with beanpostprocessors . Beforeinitialization extension Point (11). Traverse  list<beanpostprocessor> Beanpostprocessors , calling each  beanpostprocessor   "COLOR: #ffad5b; "data-wiz-span=" Data-wiz-span ">postprocessbeforeinitialization method. then, the 1624th line of code calls the initialization method: If the bean is an Initializingbean instance, the Afterpropertiesset method (12) is called, if the XML file also defines the Init-method, the Init-method (13) is called through reflection. In addition, the Spring framework tries to ensure that the same initialization method is not executed multiple times (you can try to modify Init-method to "Afterpropertiesset" to see the results of the execution). Finally, the 1633th line of code handles beanpostprocessors 's afterinitialization extension Point (+). Traverselist<beanpostprocessor> beanpostprocessors, call eachthe postprocessafterinitialization method of beanpostprocessor .  

Spring IOC Source Simple Analysis 04-bean initialization

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.