AOP Principle - Analysis of source code of AnnotationAwareAspectJAutoProxyCreator

1. Review @EnableAspectJAutoProxy

In the previous chapter, by viewing the source code of the @EnableAspectJAutoProxy annotation, it is as follows:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
    
    
 boolean proxyTargetClass() default false;
 boolean exposeProxy() default false;
}

It is known that the @EnableAspectJAutoProxy annotation is to register a component named internalAutoProxyCreator = AnnotationAwareAspectJAutoProxyCreator in the container by using @Import(AspectJAutoProxyRegistrar.class).

And also analyzed the core inheritance relationship of the AnnotationAwareAspectJAutoProxyCreato class, as follows:

AnnotationAwareAspectJAutoProxyCreator
    ->AspectJAwareAdvisorAutoProxyCreator(父类)
        ->AbstractAdvisorAutoProxyCreator(父类)
            ->AbstractAutoProxyCreator(父类)
                implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware(两个接口)

Looking at the inheritance relationship, it can be found that this class implements the Aware and BeanPostProcessor interfaces, both of which are related to the initialization of Spring beans, so it is speculated that the main processing methods of this type come from the implementation methods of these two interfaces. At the same time, this class also implements the order method.

  • BeanPostProcessor: Post-processor, that is, do something before and after the bean initialization is completed
  • BeanFactoryAware: Automatically inject BeanFactory

Next, let's take a look at the calling process of the AnnotationAwareAspectJAutoProxyCreator class. Specifically, look at what work AnnotationAwareAspectJAutoProxyCreator does as a BeanPostProcessor and what work it does as a BeanFactoryAware.

2. AbstractAutoProxyCreator class

From the inheritance relationship of the AnnotationAwareAspectJAutoProxyCreator class, it can be seen that the SmartInstantiationAwareBeanPostProcessor interface and the BeanFactoryAware interface are implemented in the AbstractAutoProxyCreator class.

So, first analyze from the AbstractAutoProxyCreator class.

It can be seen from the definition of the AbstractAutoProxyCreator class that the AbstractAutoProxyCreator class directly implements the SmartInstantiationAwareBeanPostProcessor interface and the BeanFactoryAware interface.

public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
  implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
    
    
  ...
}

Since AbstractAutoProxyCreator implements the BeanFactoryAware interface, there must be a setBeanFactory() method in the AbstractAutoProxyCreator class, as shown below:

@Override
public void setBeanFactory(BeanFactory beanFactory) {
    
    
    this.beanFactory = beanFactory;
}

@Nullable
protected BeanFactory getBeanFactory() {
    
    
    return this.beanFactory;
}

In addition, there are methods related to the BeanPostProcessor post processor in the AbstractAutoProxyCreator class, which are: postProcessBeforeInstantiation(), postProcessAfterInstantiation(), postProcessProperties(), postProcessBeforeInitialization(), postProcessAfterInitialization(). The overall source code is as follows:

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
    
    
    Object cacheKey = getCacheKey(beanClass, beanName);
    if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)){
    
    
        if (this.advisedBeans.containsKey(cacheKey)) {
    
    
            return null;
        }
        if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
    
    
            this.advisedBeans.put(cacheKey, Boolean.FALSE);
            return null;
        }
    }
    TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
    if (targetSource != null) {
    
    
        if (StringUtils.hasLength(beanName)) {
    
    
            this.targetSourcedBeans.add(beanName);
        }
        Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
        Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
        this.proxyTypes.put(cacheKey, proxy.getClass());
        return proxy;
    }
    return null;
}

@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) {
    
    
    return true;
}

@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
    
    
    return pvs;
}

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) {
    
    
    return bean;
}

@Override
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
    
    
    if (bean != null) {
    
    
        Object cacheKey = getCacheKey(bean.getClass(), beanName);
        if (this.earlyProxyReferences.remove(cacheKey) != bean) {
    
    
            return wrapIfNecessary(bean, beanName, cacheKey);
        }
    }
    return bean;
}

At this point, we have seen the implementation of BeanFactoryAware and the implementation of BeanPostProcessor post-processor in the AbstractAutoProxyCreator class.

Next, let's take a look at the AbstractAdvisorAutoProxyCreator subclass of AbstractAutoProxyCreator.

3. AbstractAdvisorAutoProxyCreator class

In the AbstractAdvisorAutoProxyCreator class, we will see the following code:

@Override
public void setBeanFactory(BeanFactory beanFactory) {
    
    
    super.setBeanFactory(beanFactory);
    if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
    
    
        throw new IllegalArgumentException(
            "AdvisorAutoProxyCreator requires a ConfigurableListableBeanFactory: " + beanFactory);
    }
    initBeanFactory((ConfigurableListableBeanFactory) beanFactory);
}

Note that the setBeanFactory() method is rewritten in the AbstractAdvisorAutoProxyCreator class. And in the setBeanFactory() method of the AbstractAdvisorAutoProxyCreator class, the setBeanFactory() method in the AbstractAutoProxyCreator class will be called first.

The initBeanFactory() method is also called in the setBeanFactory() method, and the implementation of the initBeanFactory() method is as follows:

protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    
    
    this.advisorRetrievalHelper = new BeanFactoryAdvisorRetrievalHelperAdapter(beanFactory);
}

In addition, we did not find methods related to post processors in the AbstractAdvisorAutoProxyCreator class.

Next, we continue to analyze the subclass AspectJAwareAdvisorAutoProxyCreator of the AbstractAdvisorAutoProxyCreator class.

Four, AspectJAwareAdvisorAutoProxyCreator class

By viewing the source code of the AspectJAwareAdvisorAutoProxyCreator class, we know that there is no post-processor-related code in the AspectJAwareAdvisorAutoProxyCreator class. Therefore, we continue to analyze the subclass AnnotationAwareAspectJAutoProxyCreator of the AspectJAwareAdvisorAutoProxyCreator class.

Five, AnnotationAwareAspectJAutoProxyCreator class

In the AnnotationAwareAspectJAutoProxyCreator class, we can find an initBeanFactory() method as follows:

@Override
protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    
    
    super.initBeanFactory(beanFactory);
    if (this.aspectJAdvisorFactory == null) {
    
    
        this.aspectJAdvisorFactory = new ReflectiveAspectJAdvisorFactory(beanFactory);
    }
    this.aspectJAdvisorsBuilder =
        new BeanFactoryAspectJAdvisorsBuilderAdapter(beanFactory, this.aspectJAdvisorFactory);
}

Seeing this, the friends are a little clear about the calling process of setBeanFactory, right? In fact, the calling process of setBeanFactory() is as follows: first, the setBeanFactory() method in the AbstractAdvisorAutoProxyCreator class will be executed, and the setBeanFactory() method in its parent class AbstractAutoProxyCreator will be called in the setBeanFactory() method in the AbstractAdvisorAutoProxyCreator class, and then the setBeanFactory() method in the AbstractAdvisorAutoProxyCreator class will be called. The initBeanFactory() method is called in the setBeanFactory() method. Since the initBeanFactory() method is rewritten in the subclass AnnotationAwareAspectJAutoProxyCreator, the initBeanFactory() method in the AnnotationAwareAspectJAutoProxyCreator class is finally called. In AnnotationAwareAspectJAutoProxyCreator, super.initBeanFactory(beanFactory) will be executed first; the initBeanFactory() method of the parent class AbstractAdvisorAutoProxyCreator will be called.
So it's a bit convoluted, let's take a look at a picture.

insert image description here

Note that the setBeanFactory() method in the AbstractAdvisorAutoProxyCreator class in the above figure is used as the entry point of the program call. It will call AbstractAutoProxyCreator#setBeanFactory() and AnnotationAwareAspectJAutoProxyCreator#initBeanFactory() in turn, and then call AbstractAdvisorAutoProxy by AnnotationAwareAspectJAutoProxyCreator#initBeanFactory() Creator#initBeanFactory( ).

In addition, we found no post-processor-related code in the AnnotationAwareAspectJAutoProxyCreator class.

Well, the above is the source code of the AnnotationAwareAspectJAutoProxyCreator class we analyzed. In the next article, we will start to debug the specific execution process of these source codes.

Guess you like

Origin blog.csdn.net/qq_36602071/article/details/130011086