NOTE

2.32 Autowired Principle

Spring handles dependency injection during post-processor registration and bean creation. These notes trace @Autowired through AutowiredAnnotationBeanPostProcessor.

JavaCreated Updated 1 min readhistorical

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

1. Class Inheritance Diagram

2. Implementation 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 records the elements to inject and their target classes.

  • 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