The article directories

  • One, foreword
  • Second, the BeanDefinitionRegistryPostProcessor interface
  • Three, the paper core: analysis of the spring container how to use BeanDefinitionRegistryPostProcessor interface
    • 3.1 the refresh () the fifth method: invokeBeanFactoryPostProcessors () method
    • 3.2 From AbstractApplicationContext class invokeBeanFactoryPostProcessors () method to PostProcessorRegistrationDelegate. InvokeBeanFactoryPostP Rocessors () method
    • 3.3 PostProcessorRegistrationDelegate. InvokeBeanFactoryPostProcessors () method (key, a total of seven parts)
  • Four, interview golden finger (interview language organization)
  • Five, the summary

One, foreword

Summary: What this interface does: Register beans into the Spring container (this is the core tutorial to learn, remember)



If you want to register your bean to the Spring container and let Spring do the instantiation, the usual way is as follows:

  1. XML is configured by bean nodes;
  2. Use @service, @Controller, @conponent annotations; In addition to the above methods, Spring also allows you to register specified classes in the Spring container through code, which is the main content of today’s practice. Next, we will start from spring source code, first learn the source code and then practice;

Two main points of this article

  1. Understand BeanDefinitionRegistryPostProcessor interface;
  2. Analysis of the spring container how to use BeanDefinitionRegistryPostProcessor interface;

Second, the BeanDefinitionRegistryPostProcessor interface

Key is to realize register beans feature BeanDefinitionRegistryPostProcessor interface, take a look at this interface inheritance, the diagram below:

The inheritance relationship between two back-end processor interfaces: BeanFactoryPostProcessor is the parent interface; BeanDefinitionRegistryPostProcessor is interface.

BeanDefinitionRegistryPostProcessor inherited spring BeanFactoryPostProcessor interface (that is, a blog on the interface), BeanFactoryPostProcessor’s implementation class controls the definition of the bean when its postProcessBeanFactory method is called, Therefore BeanDefinitionRegistryPostProcessor implementation class to implement the following two methods:

  1. Void postProcessBeanFactory (ConfigurableListableBeanFactory the beanFactory) throws BeansException: In the implementation of this method, it is mainly used to make some changes to the bean definition, as in the previous blog;
  2. Void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry) throws BeansException: This method is used to register more beans into the Spring container. Look closely at the BeanDefinitionRegistry interface to see what this parameter gives us:

Auric goldfinger: postProcessBeanFactory () and postProcessBeanDefinitionRegistry () the difference between postProcessBeanFactory () methods: used to make some changes in our bean definitions on a blog (core); PostProcessBeanDefinitionRegistry () method: used to register more bean in the spring container (core);

As you can see from the figure above, BeanDefinitionRegistry provides rich methods for manipulating bean definitions with code to register beans to the Spring environment. Methods for determining, registering, unregistering, and so on are all in place. When we write postProcessBeanDefinitionRegistry method content, can be used directly into the registry refs of these methods to complete the judgment and operation such as registration, the registration;

Three, the paper core: analysis of the spring container how to use BeanDefinitionRegistryPostProcessor interface

To see BeanDefinitionRegistryPostProcessor interface implementation class, where was used by the spring container:

3.1 the refresh () the fifth method: invokeBeanFactoryPostProcessors () method

As shown in the figure below, the red box in the rear invokeBeanFactoryPostProcessors method used to find out all the beanFactory processor, and invoke the processor to change the bean definition:

3.2 From AbstractApplicationContext class invokeBeanFactoryPostProcessors () method to PostProcessorRegistrationDelegate. InvokeBeanFactoryPostP Rocessors () method

Open the invokeBeanFactoryPostProcessors method, as shown below, the actual operation is entrusted PostProcessorRegistrationDelegate to do:

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
    }
Copy the code

3.3 PostProcessorRegistrationDelegate. InvokeBeanFactoryPostProcessors () method (key, a total of seven parts)

Continue seeing the invokeBeanFactoryPostProcessors PostProcessorRegistrationDelegate class methods, this method is too rich content, we only see a key, the first key shown in the diagram below the red box, Whether the current beanFactory implements BeanDefinitionRegistry:

In order to understand this problem, we should take a look at the current the beanFactory inheritance and implementation, in the application of springboot, for example, the current type is the beanFactory DefaultListableBeanFactory, to see the class diagram:

As you can see from the red box above, beanFactory implements the BeanDefinitionRegistry interface, so our focus is on the execution logic after the if condition is satisfied;

Continue to see PostProcessorRegistrationDelegate invokeBeanFactoryPostProcessors method in a class, The following section is the core logic of BeanDefinitionRegistryPostProcessor operation:

boolean reiterate = true;
while (reiterate) {
    reiterate = false; / / found out all the implementation bean name postProcessorNames = BeanDefinitionRegistryPostProcessor interface beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class,true.false);
    for(String ppName : PostProcessorNames) {// In the previous logic, beans that are PriorityOrdered and Ordered have been processed, so those that are not in processedBeans are filtered through processedBeansif(! ProcessedBeans. The contains (ppName)) {/ / according to the names and types for bean BeanDefinitionRegistryPostProcessor pp. = the beanFactory getBean (ppName, BeanDefinitionRegistryPostProcessor.class); / / put all the bean method has been invoked postProcessBeanDefinitionRegistry registryPostProcessors registryPostProcessors. In the add (pp); / / the method has been invoked postProcessBeanDefinitionRegistry all the names of the beans in processedBeans processedBeans. Add (ppName); / / pp. Perform this bean postProcessBeanDefinitionRegistry method postProcessBeanDefinitionRegistry (registry); // Change exitwhileReiterate =true; }}} / / registryPostProcessors save all the beans out postProcessBeanDefinitionRegistry method, / / now and then to perform these bean invokeBeanFactoryPostProcessors postProcessBeanFactory method (registryPostProcessors, the beanFactory); / / regularPostProcessors save is brought into the participation of all in the spring BeanFactoryPostProcessor implementation class, and it has eliminated BeanDefinitionRegistryPostProcessor implementation class, / / to make these beans now perform invokeBeanFactoryPostProcessors postProcessBeanFactory method (regularPostProcessors, the beanFactory);Copy the code

(1) all realized BeanDefinitionRegistryPostProcessor interface beans, its postProcessBeanDefinitionRegistry method will be called,

(2) and then call the postProcessBeanFactory method, so that if we customize the BeanDefinitionRegistryPostProcessor interface implementation class, Then we develop postProcessBeanDefinitionRegistry and postProcessBeanFactory method will be performed once;

At this point, our source code learning part is complete.

Four, interview golden finger (interview language organization)

Gold finger 1: with the source code parsing 001 Spring start initialization 】 the relationship between the refresh () call invokeBeanFactoryPostProcessors () method, this is the refresh () the fifth method: Change the definition of the bean (BeanFactoryPostProcessor interface).

Gold finger 2: core method: PostProcessorRegistrationDelegate invokeBeanFactoryPostProcessors () method (key, a total of seven parts) the first paragraph to paragraph 5: Handling of just realized BeanDefinitionRegistryPostProcessor interface; Paragraphs 6 through 7: processing that only implements the BeanFactoryPostProcessor interface; All together, Is the only realized BeanDefinitionRegistryPostProcessor interface treatment, only to realize the spring BeanFactoryPostProcessor interface, realize the BeanDefinitionRegistryPostProcess at the same time Or interface and BeanFactoryPostProcessor interface;

Goldfinger 3: BeanDefinitionRegistryPostProcessor class two methods BeanDefinitionRegistryPostProcessor inherited spring BeanFactoryPostProcessor interface (that is, a blog on the interface), BeanFactoryPostProcessor’s implementation class controls the definition of the bean when its postProcessBeanFactory method is called, Therefore BeanDefinitionRegistryPostProcessor implementation class to implement the following two methods:

  1. Void postProcessBeanFactory (ConfigurableListableBeanFactory the beanFactory) throws BeansException: In the implementation of this method, it is mainly used to make some changes to the bean definition, as in the previous blog;
  2. Void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry) throws BeansException: this method is used to register more bean in the spring container

Five, the summary

BeanDefinitionRegistryPostProcessor interface to complete

Play code every day, progress every day!!