Interpretation of the Spring core IOC source code the postProcessBeanFactory method that implements the beanfactory postprocessors interface

   AbstractApplicationContext. In the invokebeanfactorypostprocessors () code block, let the # PostProcessorRegistrationDelegate # class execute the logic that implements the # postProcessBeanFactory() method 👇

    protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        //The specific execution logic is in this method
        PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

        // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
        // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
        if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
            beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
            beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));

   PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); This method has two big logics, and there are three similar small logics inside the two big logics. The two large logics handle the implementation of the # BeanDefinitionRegistryPostProcessor # interface and # BeanFactoryPostProcessor # interface respectively. The three small logics deal with the implementation of the # PriorityOrdered # interface, Ordered # interface and no Order # interface in turn.

This method is very long, with more than 130 lines. Take it apart step by step.

1, The first part first refers to the implementation of BeanDefinitionRegistryPostProcessor

Let's first look at the class structure of BeanDefinitionRegistryPostProcessor 👇, It inherits the beanfactoryprocessor interface

Its method description: after the basic initialization, edit the beandefinitionregistry inside the container. At this time, the BeanDefinition has been loaded, but it has not been initialized yet. More beandefinitions are allowed to be added at this time.

(1) PriorityOrdered

            List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

            // (1)screen PriorityOrdered realization
            String[] postProcessorNames =
                    beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
            //(2)according to Order Value size sorting
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            //(3)registryProcessors Final treatment postProcessBeanFactory()
            //(4)Core: Processing postProcessBeanDefinitionRegistry()Method call
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);

(2) Ordered second priority

The code of the second step is very similar to that of the first step, that is, replace the "PriorityOrdered" with "Ordered", and de duplicate the "postProcessBeanDefinitionRegistry() method that has been executed in the first step

(3) No final processing

The third step is to execute the remaining "BeanDefinitionRegistryPostProcessor" after the first step and the third part are executed.

(4) Implement the # postProcessBeanFactory() implementation of # BeanDefinitionRegistryPostProcessor #

            // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
            invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
            invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);

As you can see in step (1) (3), registryProcessors finally processes postprocessbeanfactory (). This step is to add the # BeanDefinitionRegistryPostProcessor # object to the # registryProcessors # collection, and finally execute # postProcessBeanFactory() together.

2, The second part is the implementation of beanfactoryprocessor

Similar to the steps (1), (2) and (3) in the first part above, it is also sorted and executed according to the Order, but it is directly executed by {postProcessBeanFactory()


In order to implement the beanprocessor() method of beanprocessor(), the main step is to implement the beanprocessor() method of beanprocessor(); Beanfactory # postprocessor implementation order ()

Tags: Spring

Posted by gilgimech on Sat, 14 May 2022 15:26:36 +0300