Bean instantiation (1) preparation

refresh()

Line 551: Instantiate the bean.

To call this class method:Call to DefaultListableBeanFactory class:Line 871: Get the collection of registered BeanDefinition names. The previous XML parsing has registered beans that need spring management into the bean factory.

Line 897

doGetBean

Line 250: Get the instance from the cache first.

Line 324: Singleton by default.

createBean(beanName, mbd, args)

Line 516: doCreateBean(beanName, mbdToUse, args), calls this class method, as shown below.

doCreateBean(beanName, mbdToUse, args)

Line 556: Call this class method as shown below.


Line 1175-1177: Instantiate the FactoryBean. Line 1200-1204: Instantiate the parameterized constructor with the @Autowired annotation. Line 1207-1210: Instantiate the constructor without the @Autowired annotation. Line 1213: Instantiate the default constructor.

Instantiation FactoryBean

Method 1: Call a non-static method of another class

Methods:

Configuration label:



Line 19: The current bean label has no class attribute configured; XML parsing sets the factory-bean and factory-method property values to the corresponding properties of the current tag BD object. As follows:



The following method is then called: Line 395: Create a wrapper object BeanWrapperImpl for the bean instance.

Line 403: Check if there is a factoryBeanName for the BD object. There is a factoryBeanName.

Line 408: Create the FactoryBean object.

Line 412: access to the FactoryBean object class here is “com. Springdemo. Beans. ITMan”.

Line 413: Sets whether a static variable is false.

Following the diagram, is a method:Line 466: Get all the methods of the current class.

Line 467-472: Loop through all methods, finding non-static methods whose names are set to the factory-method attribute on the tag.

Line 483: If only one target method is found, call the Instantiate method (i.e., reflection call) and set the returned object to BeanWrapperImpl.

Line 484: The method returns BeanWrapperImpl.


Method 2: Call a non-static method of this class

Methods:



Configuration label:



It’s the same method as above. For easy reading, map again:

Line 417-418: Throw an exception if the bean tag does not set the factory-bean property or the class property.

Line 412: Gets the class.

Line 413: Sets the static variable to true.

Lines 466-484: Instantiation is consistent with method one.


Instantiate the bean through a constructor

Instantiate A FactoryBean the same way as above:Line 1200-1204: Instantiate the bean entry through the constructor. In two steps, the target constructor is found, and the instance is created by calling it reflectively.

Instantiate the parameterized constructor with the @Autowired annotation

Types of information:

Line 1200: Calls this class method.Method description:

BeanPostProcessor cycle of the application of all the BeanPostProcessor class, find out the BeanPostProcessor SmartInstantiationAwareBeanPostProcessor type, And call it determineCandidateConstructors () method.

Here to call to determineCandidateConstructors AutowiredAnnotationBeanPostProcessor class () method.

There is only one parameter constructor with an @autowired annotation

Line 262-293: Support for Lookup annotations. Line 304: Gets all the constructors for the current class. Line 316: Collection of loop constructors. Line 323: Gets the @Autowired annotation information for the current constructor. Line 344: Gets the value of the required attribute of the @AutoWired annotation (default is true). Line 352: Assign the current constructor to requiredConstructor. Line 354: Adds the current constructor to the collection.

There are multiple parameterized constructors with @Autowired annotations

Types of information:

An error is reported when running:

Line 338 :(the first loop records a constructor at line 352), and on the second loop requiredConstructor is not empty and throws an exception.

Solution: Do not assign requiredConstructor each time through the loop, simply set the required attribute of the @AutoWired annotation to false. As follows:




Instantiate a constructor without an @autowired annotation

Types of information:



Method summary: If there is only one constructor without the @Autowired annotation, it is the end goal. If there are more than one, null will be returned, and the default construct 1213 will be called, at which point an error will be reported if no default construct is written.


At this point the constructor is found and then called to create the instance. Again, this method: Line 1203: Call this class methodThen call the autowireConstructor() method of the ConstructorResolver class.

Method description:

  1. A constructor that directly reflects calls to create instances.
  2. Multiple constructors, sorted by the number of constructors’ parameters, will eventually call the one with the most parameters.

conclusion

Beans can be instantiated in two ways:

  • FactoryBean

    Set factory-bean and factory-method properties in the XML configuration file bean tag.

    1. Call non-static methods of other classes.
    2. Call static methods of this class.
  • Instantiate beans through constructors
    1. Select the constructor with the @AutoWired annotation first. If there are more than one, you can set required to false, and Spring will choose the instantiated bean with the most parameters.
    2. If there is no constructor annotated with @autoWired, Spring selects a unique constructor. If there are more than one, Spring selects > and calls the default constructor to instantiate the bean. If there is no default constructor, an error is reported.
    3. If there is no custom constructor in the class, Spring calls the default constructor to instantiate the bean.

Note: this article through the source code + line description of the way to describe, if not understand can leave a message. This article is only a personal learning record, there are mistakes, please correct.

Annotation collection, dependency injection (DI)