Spring source code analysis-spring post processor embedded point design

Preface

In the loading phase of the spring container, there are some very important designs similar to buried points (this concept is not uniformly called, and some are called "post processors"). For the time being, this article uses the literal meaning of the interface as the PostProcessor. The design of these post processors extends the functionality of the spring container. It allows customizing different types of post-processors, realizing external intervention in processing nodes and loading timing of beans and bean definitions, so as to meet the individual needs of the project.

Post processor in Spring

Spring source code analysis-spring post processor embedded point design
The three most important components are: ConfigurationClassPostProcessor, AutowiredAnnotationBeanPostProcessor, CommonAnnotationBeanPostProcessor, etc.
Let's first take a look at the buried points of the above components in the spring source code (processing node design)
BeanDefinition registration stage
ClassPathXmlApplicationContext container implementation, registration points for the above three components
Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design
AnnotationConfigApplicationContext container implementation, registration points for the above three components
Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design
Trigger the BeanDefinition intervention phase
Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design

Specific processing flow
Spring source code analysis-spring post processor embedded point design

Corresponding source code implementation
Spring source code analysis-spring post processor embedded point design

Bean instantiation pre-stage
Spring source code analysis-spring post processor embedded point design

PostProcessorRegistrationDelegate.registerBeanPostProcessors() instantiates the BeanPostProcessor component in advance for later use when instantiating other beans
Spring source code analysis-spring post processor embedded point design

Bean instantiation post-stage br /> AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation() handles @Bean, factoryMethod and other external instantiation buried points
Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design
AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors() collects information such as dependency properties and method injection of bean objects
Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design
AbstractAutowireCapableBeanFactory.populateBean() bean object dependency injection assignment
Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design
AbstractAutowireCapableBeanFactory.initializeBean() generates Aop dynamic proxy related processing for bean objects
Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design

Spring source code analysis-spring post processor embedded point design

Application of post processor

The three post-processors are not used much in actual projects, but some projects will use the following scenarios: 1. After beandefinition is registered, the beandefinition in the container needs to be enhanced or intervened at runtime; 2. In the process of bean instantiation, a certain class does not want to be instantiated in spring; 3. Hope that after the container starts, some global business logic will be done; 4. The development of spring plug-ins needs to be integrated or docked with spring. Here are some examples of custom post processor scenarios:
BeanDefinitionRegistryPostProcessor extension
Spring source code analysis-spring post processor embedded point design
BeanFactoryPostProcesser extension
Spring source code analysis-spring post processor embedded point design
InitializingBean, Bean instantiation extension
Spring source code analysis-spring post processor embedded point design

Concluding remarks

The source code of the spring post processor is temporarily shared here. For more post processor usage scenarios, those who are interested can take the time to study the source code of the spring container loading process. This kind of thing, the usage scenario is not as common as the annotation, but it also has its use (especially for design or architecture). You can choose according to your project and job role. That's it for today, for more dry goods of spring source code, please continue to pay attention!

Guess you like

Origin blog.51cto.com/14815984/2532795