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 has not yet been instantiated.

Function: Adds or modifies Bean definition information.

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;
Copy the code

// call here

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

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

CachingMetadataReaderFactoryPostProcessor

SharedMetadataReaderFactoryContextInitializer inner classes. If relative electron has more intuitive understanding, it can also refer to its format as follows:

Game Agent: www.walajiao.com

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) { } }Copy the code

We found that its role is to add a org. Springframework. Boot. Autoconfigure. InternalCachingMetadataReaderFactory Bean definition of information, Then it adds 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)); }}Copy the code

Click on it and find out (there’s only one Check object right now) what it does is it takes all the ComponentScan attributes, and then it checks if it’s org.springframework or org, If yes, an exception message is printed. 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, it is called once and not 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.

invokeBeanDefinitionRegihttp://www.walajiao.com/stryPostProcessors(currentRegistryProcessors, registry);

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

This method is also called with the Bean definition information loaded and no Bean instantiated. But the official note says it’s ok to instantiate the Bean first here.

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 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;
Copy the code

} init your Own eventListenerFactories property.

Summary above: Spring BeanDefinitionRegistryPostProcessor and Spring BeanFactoryPostProcessor implementation class method call sequence is – > call first BeanDefinitionRegistryPostProcessor postProcessBeanDefinitionRegistry method, order is PriorityOrdered > Ordered > other. The order is judged internally based on the order value. Call again BeanDefinitionRegistryPostProcessor postProcessBeanFactory method, order judgment as well. Finally, the postProcessBeanFactory method of BeanFactoryPostProcessor is executed.