[spring annotation driven development] - BeanFactoryPostProcessor

Article directory

Source address of demo of this blog
https://github.com/suchahaerkang/spring-annotation.git

1 BeanFactoryPostProcessor

The BeanFactoryPostProcessor component is similar to the BeanPostProcessor component, Previous blog We know that the function of the bean processor component is to process before and after bean creation initialization. When is the execution time of the BeanFactoryPostProcessor component? Let's take a look at the source code

1.2 code test

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-05 15:46
 */
public class Blue {

    public Blue() {
        System.out.println("Blue Component creation...");
    }
}

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-14 9:01
 */
@ComponentScan("com.wolfx.ext")
@Configuration
public class ExtConfig {

    @Bean
    public Blue blue(){
        return new Blue();
    }
}

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-14 9:02
 */
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {


    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanFactoryPostProcessor...postProcessBeanFactory()Method execution...");
        //Get all bean definition information names in the container
        String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));
    }
}

Write a test case

@Test
public void test01(){
     AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ExtConfig.class);
 }

test result

1.2 source code analysis

Create IOC container, refresh container

invokeBeanFactoryPostProcessors(beanFactory)

Let's take a look at the operation in the invokebenfactorypostprocessors (beanfactory) method





2 BeanDefinitionRegistryPostProcessor

BeanDefinitionRegistryPostProcessor component inherits BeanFactoryPostProcessor component

Look at the source code of BeanDefinitionRegistryPostProcessor

2.1 code test

Add a component of MyBeanDefinitionRegistryPostProcessor to implement the BeanDefinitionRegistryPostProcessor interface based on the above code

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-14 10:41
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor...postProcessBeanDefinitionRegistry()Method execution...");
        int cout = registry.getBeanDefinitionCount();
        System.out.println("Container defined"+ cout +"Components");
        RootBeanDefinition beanDefinition = new RootBeanDefinition(Blue.class);
        registry.registerBeanDefinition("Blue", beanDefinition);
    }

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanDefinitionRegistryPostProcessor...postProcessBeanFactory()Method execution...");
        int cout = beanFactory.getBeanDefinitionCount();
        System.out.println("Container defined"+ cout +"Components");
    }
}

test result

2.2 source code analysis





3 Summary

  • BeanFactoryPostProcessor is the postprocessor of BeanFactory. Function and execution time:
    • Effect: after the BeanFactory standard is initialized, it calls to customize and modify the contents of BeanFactory.
    • Execution time: all bean definitions have been saved and loaded into beanFactory, but the bean instance has not been created yet
  • BeanDefinitionRegistryPostProcessor inherits BeanFactoryPostProcessor. Function and execution time:
    • Function: add some additional components to the container
    • Execution time: when all bean definition information will be loaded and the bean instance has not been created, it takes precedence over the execution of BeanFactoryPostProcessor
83 original articles published, 32 praised, 90000 visitors+
Private letter follow

Tags: github Spring git

Posted on Fri, 13 Mar 2020 23:57:41 -0400 by halex