NOTE

2.32 Autowired Principle

Class inheritance diagram and principle analysis of Spring's AutowiredAnnotationBeanPostProcessor.

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Class Inheritance Diagram

2. Principle Analysis

  1. When the Spring container refreshes, there are two key steps: registerBeanPostProcessors and finishBeanFactoryInitialization.
  2. registerBeanPostProcessors injects the AutowiredAnnotationBeanPostProcessor BeanPostProcessor into the Spring container. It has two key methods:
  • postProcessMergedBeanDefinition (from MergedBeanDefinitionPostProcessor)

The purpose of this method is to encapsulate information about fields that need dependency injection into the InjectionMetadata class. InjectionMetadata contains which elements need to be injected and into which target class those elements should be injected.

  • postProcessPropertyValues (from InstantiationAwareBeanPostProcessor)

The purpose of this method is to complete dependency-property injection.

  1. After finishBeanFactoryInitialization creates the Bean instance, it calls AutowiredAnnotationBeanPostProcessor.postProcessMergedBeanDefinition. Then, when assigning values to properties, it calls AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues. Finally, it obtains the Bean instance to inject through BeanFactory.getBean(beanName, beanClass) and completes property injection through reflection.

3. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub