# # 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:
- Beannameaware ' s Setbeanname
- Beanclassloaderaware ' s Setbeanclassloader
- Beanfactoryaware ' s Setbeanfactory
- Environmentaware ' s setenvironment
- Embeddedvalueresolveraware ' s Setembeddedvalueresolver
- Resourceloaderaware ' s Setresourceloader (only applicable if running in an application context)
- Applicationeventpublisheraware ' s Setapplicationeventpublisher (only applicable if running in an application context)
- Messagesourceaware ' s Setmessagesource (only applicable if running in an application context)
- Applicationcontextaware ' s Setapplicationcontext (only applicable if running in an application context)
- Servletcontextaware ' s Setservletcontext (only applicable if running in a Web application context)
- Postprocessbeforeinitialization Methods of Beanpostprocessors
- Initializingbean ' s Afterpropertiesset
- A custom Init-method definition
- Postprocessafterinitialization Methods of Beanpostprocessors
On shutdown of a bean factory, the following lifecycle methods apply:
- Postprocessbeforedestruction Methods of Destructionawarebeanpostprocessors
- Disposablebean ' s Destroy
- 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