Several scenarios for Bean instantiation
1、Beandefinitionregistrypostprocessor instantiation: StandardBeanfactorypostprocessorThe extension,Beanfactorypostprocessoris used to further define the registeredBeandefinition,IoCContainer essence isBeanManagement, soBeanfactorypostprocessoritself isBean, to theBeanfactorypostprocessorOfBeandefinitionFurther definition is achieved byBeandefinitionregistrypostprocessorTo register,beandefinitionregistrypostprocessorioc container most instantiated class bean configurableapplicationcontext (applicationcontext sub-interface) Implementation class call refresh () method call invokebeanfactorypostprocessors ( Beanfactory); method is instantiated.
2,beanfactorypostprocessor instantiation : After beandefinitionregistrypostprocessor is instantiated. They are instantiated in the same way , only sequentially.
[Java]View PlainCopy
- Postprocessorregistrationdelegate.invokebeanfactorypostprocessors (
- Configurablelistablebeanfactorybeanfactory, list<beanfactorypostprocessor> beanFactoryPostProcessors)
3、Beanpostprocessorss instantiation:BeanpostprocessorsIsIocContainerBeanManaged extension points that define thebean callback method before and after instantiation, It is certainly more than ordinary bean early. configurableapplicationcontextregisterbeanpostprocessors (beanfactory) method is responsible for beanpostprocessorsbeanpostprocessor The class of Postprocessorregistrationdelegate.registerbeanpostprocessors (beanfactory,this);
4,messagesource type bean instantiation : Corresponding to the refresh () call Initmessagesource ();
5、Applicationeventmulticaster (event broadcast) type bean instantiation : Responsible for Applicationevent event broadcast bean,refresh () call initapplicationeventmulticaster (); bean is not required to be configured , but it is common in ioc
6. Subclasses of the Abstractapplicationcontext class may instantiate a personalized Bean, andrefresh () calls Onrefresh (); method to do this thing.
7.applicationlistener type Bean instantiation:Applicationeventmulticaster is responsible for event broadcast, it has to register the broadcast object, all Applicationlistener will be in registerlisteners (); instantiated.
1~7corresponding to theBeanIt's all special.bean except 6beanfactory String[] Getbeannamesfortype (class<?> type, Booleanincludenonsingletons, Boolean alloweagerinit); and these bean set lazy-initsingleton
8, non - lazy-init and Singleton Bean instantiation: Call inishbeanfactoryinitialization (beanfactory);
[Java]View PlainCopy
- protected void Finishbeanfactoryinitialization (Configurablelistablebeanfactory beanfactory) {
- The Bean is instantiated first when there is a container conversion service Bean
- if (Beanfactory.containsbean (conversion_service_bean_name) &&
- Beanfactory.istypematch (Conversion_service_bean_name, Conversionservice. Class)) {
- Beanfactory.setconversionservice (
- Beanfactory.getbean (Conversion_service_bean_name, Conversionservice. Class));
- }
- //instantiation of Loadtimeweaveraware type Bean
- string[] Weaverawarenames = Beanfactory.getbeannamesfortype (loadtimeweaveraware. Class, false, false);
- For (String weaverawarename:weaverawarenames) {
- Getbean (Weaverawarename);
- }
- //stop using the temporary class loader
- Beanfactory.settempclassloader (null);
- //cache all registered Beandefinition metadata in the container in case it is modified
- Beanfactory.freezeconfiguration ();
- //Instantiate all remaining non-lazy-init singleton beans
- Beanfactory.preinstantiatesingletons ();
- }
9OtherBeanInstantiate a scenario:A.Above1-8corresponding to theBeanInstantiation relies on otherBeanTime, theBeanis instantiated at the same time (this analysisCreatebean ()B. The singleton Bean that is lazily instantiated is instantiated the first time it is instantiated by Getbean (), c.prototype each call Getbean () is instantiated when the other scope is instantiated for the first time in its life cycle through Getbean () .
Several scenarios of spring--IOC container Bean instantiation