Instantiation of beans (2) main logic

teasers

Review of the previous article:

Line 556: At this point the instance object already exists in heap memory, but the attribute has not yet been assigned. Line 568: Annotation collection, dependency injection

Annotations to collect

applyMergedBeanDefinitionPostProcessors

This method is a typical application of PostProcessor. Loop all the PostProcessor, call MergedBeanDefinitionPostProcessor postProcessMergedBeanDefinition of ().

1. CommonAnnotationBeanPostProcessor

Complete the collection of @Resource annotations and support for @postConstruct and @Predestroy annotations.

Note: in the first place in the constructor to the parent class InitDestroyAnnotationBeanPostProcessor set the annotation information.

Line 294: Scan @postconstruct, @predestroy.

Line 232: Loop through all the methods in the class using the ReflectionUtils utility class.

Lines 233-234: Wrap the @postconstruct annotation as a LifecycleElement object and add it to the map, if any.

Lines 240-241: If there is a @predestroy annotation, wrap it as a LifecycleElement object and add it to the map.

Line 254: Finally wrap a class, a collection of annotation methods with @postConstruct and @PreDestroyLifecycleMetadata objectTo return.

Line 295: Scan the @rsource annotation.

Line 366: All in the loop classattributeIf there is an @rSource annotation, wrap it as a ResourceElement object and add it to the collection.

Line 389: all in the loop classmethodsIf there is an @rSource annotation, wrap it as a ResourceElement object and add it to the collection.

Line 436: Finally wrap classes, and methods and properties with @rsource annotationsInjectionMetadataObject and return.


2. AutowiredAnnotationBeanPostProcessor

A collection of attributes and methods for @autowired and @Value annotations.

The collection process is similar to the @rsource annotation above.

At this point, the collection of annotations is complete.

Dependency Injection (DI)

populateBean

To call this class method:

Line 1382: Application of BeanPostProcessors.

Implement InstantiationAwareBeanPostProcessor classes, custom control dependency injection

The sample application


Line 1442: Inject logic in the form of XML configuration file tags.

Line 1425: Old version logic.

Line 1420: Application of BeanPostProcessors.

Call postProcessProperties InstantiationAwareBeanPostProcessor class ()

AutowiredAnnotationBeanPostProcessor classes finish to @ the Resource the @autowired annotation injection.Line 397: Get the annotation information collected earlier.

Line 399: Called into the InjectionMetadata class.

Line 130: The collection of @autowired @rsource collected earlier in the loop.

The injection is done by reflection.


conclusion

Annotations to collect

  1. CommonAnnotationBeanPostProcessor collect @ PostConstruct, @ PreDestroy, @ Resource annotation.
  2. AutowiredAnnotationBeanPostProcessor collect @autowired, @ Value annotation.
The collection process is completed using the BeanPostProcessor.Copy the code

Dependency Injection (DI)

  1. Properties and methods with @autoWired, @Value, and @Resource annotations are injected through reflection.

When injection refers to an object, getBean() is triggered to instantiate the target object.

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.

Instantiation of beans (2) main logic