1, Spring Spring BeanFactoryPostProcessor of extension point 1.1, the is actually inherited Spring BeanFactoryPostProcessor BeanDefinitionRegistryPostProcessor, And in a postProcessBeanDefinitionRegistry method is added to the original method.

void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

This method is called when all the regular Bean definition information is loaded but not yet instantiated.

Function: Add and modify the definition information of the Bean.

Source code call: PostProcessorRegistrationDelegate invokeBeanFactoryPostProcessors method, The spring BeanFactoryPostProcessor here is in the initialization added to beanFactoryPostProcessors kind of some kind.

for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {

            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                BeanDefinitionRegistryPostProcessor registryProcessor =
                        (BeanDefinitionRegistryPostProcessor) postProcessor;

// Call here

registryProcessor.postProcessBeanDefinitionRegistry(registry); registryProcessors.add(registryProcessor); } else { regularPostProcessors.add(postProcessor); }}

Let’s look at some of the subclasses that Spring loads by default:

CachingMetadataReaderFactoryPostProcessor

SharedMetadataReaderFactoryContextInitializer inner classes.

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

}

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

this.register(registry);

this.configureConfigurationClassPostProcessor(registry);

}

private void register(BeanDefinitionRegistry registry) {

BeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(SharedMetadataReaderFactoryContextInitializer.SharedMetadataReaderFactoryBea n.class, SharedMetadataReaderFactoryContextInitializer.SharedMetadataReaderFactoryBean::new).getBeanDefinition(); registry.registerBeanDefinition(&Rth; org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory&Rth; , definition); } private void configureConfigurationClassPostProcessor(BeanDefinitionRegistry registry) { try { BeanDefinition definition = registry.getBeanDefinition(&Rth; org.springframework.context.annotation.internalConfigurationAnnotationProcessor&Rth;) ; definition.getPropertyValues().add(&Rth; metadataReaderFactory&Rth; , new RuntimeBeanReference(&Rth; org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory&Rth;) ); } catch (NoSuchBeanDefinitionException var3) { } }

We found that its role is to add a org. Springframework. Boot. Autoconfigure. InternalCachingMetadataReaderFactory Bean definition of information, And then we add a propertyValue-> metadatareaderFactory

ConfigurationWarningsPostProcessor

ConfigurationWarningsApplicationContextInitializer inner classes.

@Override

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { for (Check check : this.checks) { String message = check.getWarning(registry); if (StringUtils.hasLength(message)) { warn(message); } } } private void warn(String message) { if (logger.isWarnEnabled()) { logger.warn(String.format(&Rth; %n%n** WARNING ** : %s%n%n&Rth; , message)); }}

If you click on it and find (there’s only one Check object right now) what it does is it gets all the ComponentScan attributes and it checks if it’s org.springframework or org, Your applicationContext is unlikely to start due to a @ComponentScan of.

Code go down, the following three call BeanDefinitionRegistryPostProcessor, invoke rule is called first realized PriorityOrdered interface classes, Ordered and then call the class of the interface, Finally is the remaining BeanDefinitionRegistryPostProcessor class. If both PriorityOrdered and Ordered are implemented, then being called once will not be called again.

Through this method to call postProcessBeanDefinitionRegistry traversal. Call BeanDefinitionRegistryPostProcessor is here in the beanFactory take all matching types, not be the same as the above.

invokeBeanDefinitionRegiwww.walajiao.comstryPostProcessors(currentRegistryProcessors, registry);

1.2, spring BeanFactoryPostProcessor void postProcessBeanFactory (ConfigurableListableBeanFactory the beanFactory) throws BeansException;

This method is called when the Bean definition information is loaded and no beans have been instantiated yet. But the official comments say that beans can be instantiated here first.

This allows for overriding or adding properties even to eager-initializing beans.

Then the agency continues to go down, invokeBeanFactoryPostProcessors is call postProcessBeanFactory method callback, Here is invoked BeanDefinitionRegistryPostProcessor type postProcessBeanFactory method above, Because BeanDefinitionRegistryPostProcessor is inherited spring BeanFactoryPostProcessor agents in the game interface, and also no default implementation parent interface method, So in time and realize BeanDefinitionRegistryPostProcessor interface to realize postProcessBeanFactory method.

Continue to go down, and BeanDefinitionRegistryPostProcessor class is the same, it take from the beanFactory all spring BeanFactoryPostProcessor type of Bean, Then according to the implementation PriorityOrdered, Ordered sequence calls, use invokeBeanFactoryPostProcessors method to traverse call postProcessBeanFactory method.

Take a look at some of the BeanFactoryPostProcessors that Spring loads by default.

EventListenerMethodProcessor

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {

    this.beanFactory = beanFactory;
    Map<String, EventListenerFactory> beans = beanFactory.getBeansOfType(EventListenerFactory.class, false, false);
    List<EventListenerFactory> factories = new ArrayList<>(beans.values());
    AnnotationAwareOrderComparator.sort(factories);
    this.eventListenerFactories = factories;
}

It initializes its own EventListenerFactories property.

Above summary: Spring BeanDefinitionRegistryPostProcessor and Spring BeanFactoryPostProcessor implementation class method call sequence is – > call first BeanDefinitionRegistryPostProcessor postProcessBeanDefinitionRegistry method, order is PriorityOrdered > Ordered > other. The order is determined internally based on the order value. Call again BeanDefinitionRegistryPostProcessor postProcessBeanFactory method, order judgment as well. Finally, execute the PostProcessBeanFactory method of BeanFactoryPostProcessor.