Several scenarios of spring--IOC container Bean instantiation

Source: Internet
Author: User

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
    1. Postprocessorregistrationdelegate.invokebeanfactorypostprocessors (
    2. 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
  1. protected void Finishbeanfactoryinitialization (Configurablelistablebeanfactory beanfactory) {
  2. The Bean is instantiated first when there is a container conversion service Bean
  3. if (Beanfactory.containsbean (conversion_service_bean_name) &&
  4. Beanfactory.istypematch (Conversion_service_bean_name, Conversionservice. Class)) {
  5. Beanfactory.setconversionservice (
  6. Beanfactory.getbean (Conversion_service_bean_name, Conversionservice.   Class));
  7. }
  8. //instantiation of Loadtimeweaveraware type Bean
  9. string[] Weaverawarenames = Beanfactory.getbeannamesfortype (loadtimeweaveraware.   Class, false, false);
  10. For (String weaverawarename:weaverawarenames) {
  11. Getbean (Weaverawarename);
  12. }
  13. //stop using the temporary class loader
  14. Beanfactory.settempclassloader (null);
  15. //cache all registered Beandefinition metadata in the container in case it is modified
  16. Beanfactory.freezeconfiguration ();
  17. //Instantiate all remaining non-lazy-init singleton beans
  18. Beanfactory.preinstantiatesingletons ();
  19. }

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

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.