AOP principle - in-depth analysis of AnnotationAwareAspectJAutoProxyCreator execution timing

As mentioned in the previous chapter, the @EnableAspectJAutoProxy annotation uses AspectJAutoProxyRegistrar to create and register the AnnotationAwareAspectJAutoProxyCreator post-processor in the container, so when other beans are created later, the post-processor can intercept these beans and perform some processing.

This chapter mainly talks about AnnotationAwareAspectJAutoProxyCreator intercepting the creation process of other beans

1. Call finishBeanFactoryInitialization(beanFactory) in refresh()

After registering all BeanPostProcessors in the previous chapter, all remaining single-instance beans will be instantiated

insert image description here

2. Call beanFactory.preInstantiateSingletons() in finishBeanFactoryInitialization

Instantiate all remaining single instance beans mainly in the beanFactory.preInstantiateSingletons() method
insert image description here

List< String> beanNames = new ArrayList<>(this.beanDefinitionNames); The names of all beans are stored in this collection, and the for loop will traverse the collection once to create the remaining single instance beans. First, the bean of the configuration class MainConfigOfAOP will be created.

insert image description here

The configuration class is also a bean

insert image description here

3. Call getBean()->doGetBean()

The first half of doGetBean() will check in advance whether the single instance bean has been registered in the singleton pool. If registered, the bean will be wrapped and returned directly.

insert image description here

If there is no singleton in the pool, execute getSingleton(String beanName, ObjectFactory<?> singletonFactory) in the second half of doGetBean() to create a bean.

insert image description here

4. Call getSingleton(String beanName, ObjectFactory<?> singletonFactory)

insert image description here
insert image description here

ObjectFactory<?> The parameter of singletonFactory is a lambda expression. When executing singletonFactory.getObject(), the method createBean(beanName, mbd, args) will be called to create the bean.

insert image description here

5. Call createBean(beanName, mbd, args)

In the createBean(beanName, mbd, args) method, the bean definition information (bean definition information such as singleton, non-abstract, non-lazy loading, etc.) will be obtained first, and then the bean name and bean definition information will be passed in to prepare Execute the resolveBeforeInstantiation(beanName, mbdToUse) method.

insert image description here

6. Call resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd)

The role of the resolveBeforeInstantiation(beanName, mbdToUse) method is to give BeanPostProcessors a chance to return a proxy object to replace the target object that needs to be created.

insert image description here

7. Call applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName)

The applyBeanPostProcessorsBeforeInstantiation method will traverse all BeanPostProcessors of the InstantiationAwareBeanPostProcessor type, just as AnnotationAwareAspectJAutoProxyCreator is the BeanPostProcessors of the InstantiationAwareBeanPostProcessor type, so the postProcessBeforeInst in AnnotationAwareAspectJAutoProxyCreator will be executed next anti-method.

insert image description here

Those who implement the InstantiationAwareBeanPostProcessor interface will implement the following methods.

insert image description here

8. Call postProcessBeforeInstantiation(Class<?> beanClass, String beanName)

Note: What is executed here is the postProcessBefore Instantiation in the InstantiationAwareBeanPostProcessor interface, which is different from the postProcessbefore Initialization in the BeanPostProcessor interface . Because AnnotationAwareAspectJAutoProxyCreator implements the SmartInstantiationAwareBeanPostProcessor interface, and the SmartInstantiationAwareBeanPostProcessor interface inherits the InstantiationAwareBeanPostProcessor interface, so postProcessBefore Instantiation is executed .
Difference:
postProcessBefore Instantiation is to intercept before instantiating the bean to check whether the proxy object can be returned instead of the creation of the target object;
postProcessbefore Initialization is to process before the bean is initialized;

So this step is the execution time of AnnotationAwareAspectJAutoProxyCreator. The function of AnnotationAwareAspectJAutoProxyCreator is to intercept and check whether the proxy object can be returned before instantiating all remaining beans.

insert image description here

public class AnnotationAwareAspectJAutoProxyCreator extends AspectJAwareAdvisorAutoProxyCreator
->public class AspectJAwareAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator
->public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyCreator
->public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware 
->public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor

Nine, fall back to createBean ()

Since the first created bean is the MainConfigOfAOP configuration class, no proxy object has been created, so resolveBeforeInstantiation(beanName, mbdToUse) returns null, and then the doCreateBean() method will be executed to continue creating objects. The process of the doCreateBean() method is similar to that in the previous chapter, so it will not be analyzed here. The final created bean will be stored in the singleton pool.

insert image description here

おすすめ

転載: blog.csdn.net/qq_36602071/article/details/130024500