Spring relies on injected source reading notes

Source: Internet
Author: User

First, the call stack

A typical spring-dependent injection call stack:

    1. Defaultlistablebeanfactory:getbean ()
    2. Abstractbeanfactory:dogetbean ()
    3. Abstractautowirecapablebeanfactory:createbean ()
    4. Abstractautowirecapablebeanfactory:createbeaninstance ()
    5. Simpleinstantiationstategy:instantiate ()
    6. Abstractautowirecapablebeanfactory:populatebean ()
    7. Abstractautowirecapablebeanfactory:applypropertyvalues ()
    8. Beandefinitionvalueresolver:resolvevalueifnecessary ()
    9. Beandefinitionvalueresolver:resolvereference ()
Ii. Dependency Injection Process
  1. Getbean: Dependency Injection of ingress, processing parameters call Dogetbean ()
  2. Dogetbean: Check if you need to create a bean and get beandefinition;
    • the processing of a singleton bean , which can be taken from the singleton cache, does not need to be created. The process of finding the cache: find it first, if it is singletonObjects not found, and the bean that is being created, then find it from, and earlySingletonObjects still not find it, by singletonFactories getting it.
    • the cyclic dependency detection of the prototype pattern Bean , if it detects that it is being created ( prototypesCurrentlyInCreation exists in [ threadLocal ]), throws an exception because it is likely that a circular dependency has occurred.
    • recursively finds Beandefinition, the Bean's definition does not exist in the current beanfactory, and Getbean is called recursively to the parent beanfactory until it is found.
    • merges the parent beandefinition to get rootbeandefinition, if the bean definition obtained is a child bean definition, Then getmergedlocalbeandefinition merges it with the parent bean definition into a root bean definition
    • legality detection of rootbeandefinition : whetherabstract
    • The DependsOn explicitly declared in the bean definition is initialized (recursively): And the dependency is registered, and an exception is thrown if a circular dependency is detected.
    • creating worker processing for beans
      • Singleton Bean:
        1. singletonObjectsLocked
        2. Add the Beanname to the singletonsCurrentlyInCreation middle
        3. Create Bean
        4. singletonsCurrentlyInCreationremoving Beanname from
        5. Release singletonObjects Lock
        6. If an instance is created in the procedure, put it in, and singletonObjects registeredSingletons remove it from singletonFactories earlySingletonObjects ,
        7. If an exception occurs in the procedure, remove it from,,, and singletonObjects registeredSingletons singletonFactories earlySingletonObjects
        8. ProcessingFactoryBean
      • Prototype Pattern Bean:
        1. prototypesCurrentlyInCreationadding beanname to the middle
        2. Create Bean
        3. prototypesCurrentlyInCreationremoving Beanname from
        4. ProcessingFactoryBean
      • The remaining scope bean processing:
        1. Detect if scope exists
        2. Call the Scope.get () method and pass in the GetObject () callback. The processing in the callback is basically consistent with the prototype.
        3. ProcessingFactoryBean
    • Process The return data type to detect if it can be converted to Requiredtype
  3. Createbean: Create a Bean instance, initialize the instance, call Postprocessors, etc.
    • parse out the class type , i.e. parse out a class<?>, and assign to Rootbeandefinition. But can not be copied directly, need to copy a new rootbeandefinition, because Mergedbeandefinition is shared, there may be dynamically resolved class type, direct assignment may be problematic.
    • preprocessing overrides the method , detects whether the overridden method exists, and determines whether it is overload.
    • called once postprocessors is called InstantiationAwareBeanPostProcessor postProcessBeforeInstantiation , it is possible to return a bean proxy. If the agent for the Bean is returned, it executes the Beanprocessor Postprocessafterinitialization method directly and returns.
    • Call Hook Method Docreatebean
  4. Docreatebean
    • Remove a bean from the same name from the cache with the same name factoryBeanInstanceCache
    • To create a bean instance createbeaninstance
    • called again MergedBeanDefinitionPostProcessor by the postprocessors call.postProcessMergedBeanDefinition
    • Singleton loop-dependent processing: Put the instance you just created (uninitialized) into the cache if it is a singleton bean and allow circular dependencies, you will just create a good instance, put singletonFactories in and registeredSingletons earlySingletonObjects Remove the same name bean from the
    • Bean Initialization Populatebean, initializes the resulting bean instance with the property values in the bean definition
    • perform the initialization of the spring extension Initializebean
      • invokeAwareMethodsIf the bean implements some sub-interfaces of aware, such as Beannameaware, set the corresponding property to the bean
      • Call The Postprocessbeforeinitialization method of Postprocessors call Beanpostprocessor again
      • invokeInitMethods
        • If the bean implements the InitializingBean interface, execute the Afterpropertiesset () callback
        • If Init-method is explicitly customized in the bean definition, a Initmethod callback (called by reflection) is executed once
      • Call The Postprocessafterinitialization method of Postprocessors call Beanpostprocessor again
    • Registering the bean Destruction method callback
  5. Createbeaninstance: Create a bean instance with different policies
    • Factory method : If Factory-mothod is explicitly declared in the bean definition, the factory method is called to create the instance.
    • Constructors : Selecting the appropriate constructor instantiation
    • CGLIB : Using CGLIB instantiation
  6. Populatebean
    • Get the property of the bean definition values
    • Call once postprocessors calling postprocessafterinstantiation method of instantiationawarebeanpostprocessor
    • handling Autowire injection autowirebyname or autowirebytype
    • is called once postprocessors calls instantiationawarebeanpostprocessor Postprocesspropertyvalues method
    • to inject properties Applypropertyvalues, attribute value injection, parse out the run-time bean References to other beans in the factory.
      • Gets the beandefinitionvalueresolver.
      • creates a new list of propertyvalue as a container for the parsed propertyvalue. Must be a deep copy, otherwise it will be modified to the property values in the original bean definition.
      • Parse Property value:resolvevalueifnecessary ()
        for different types of value objects, there are different convert methods, If value is of type runtimebeanreference, call the Getbean method recursively to get the bean. The
      • uses the Beanwrapper Setpropertyvalues method to assign values to the bean's properties in bulk. Finally, the Setxxx () method of the calling Bean is assigned a value.
Iii. postprocessors and key method execution sequence
    1. " instantiationawarebeanpostprocessor " postprocessbeforeinstantiation ()
    2. createbeaninstance
    3. " mergedbeandefinitionpostprocessor " postprocessmergedbeandefinition ()
    4. " instantiationawarebeanpostprocessor " postprocessafterinstantiation ()
    5. autowirebyname ; autowirebytype
    6. " instantiationawarebeanpostprocessor " postprocesspropertyvalues ()
    7. applypropertyvalues
    8. " beanpostprocessor " postprocesspropertyvalues ()
    9. " instantiationawarebeanpostprocessor " postprocessbeforeinitialization ()
    10. invokeinitmethods
    11. " instantiationawarebeanpostprocessor " postprocessafterinitialization ()

Spring relies on injected source reading notes

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.