Hello everyone, I am Glacier ~~
The “Spring Notes series” feature, which has been suspended for a long time, is finally updated, and we continue to update the previous article. In “[Spring Annotation Driver development] Two dogson let me tell him about @enableAspectJAutoProxy annotation” article, we by looking at the @enableAspectJAutoProxy annotation source, as shown below.
package org.springframework.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)
public @interface EnableAspectJAutoProxy {
boolean proxyTargetClass(a) default false;
boolean exposeProxy(a) default false;
}
Copy the code
To learn that, @ EnableAspectJAutoProxy annotations are using @ Import (AspectJAutoProxyRegistrar. Class) for the container to register a name called internalAutoProxyCreator = AnnotationAwareAspectJAutoProxyCreator components.
And we also analyzed the AnnotationAwareAspectJAutoProxyCreato class hierarchy of the core, as shown below.
AnnotationAwareAspectJAutoProxyCreator --AspectJAwareAdvisorAutoProxyCreator --AbstractAdvisorAutoProxyCreator - AbstractAutoProxyCreator - ProxyProcessorSupport, SmartInstantiationAwareBeanPostProcessor BeanFactoryAwareCopy the code
Looking at the inheritance relationship, we can see that this class implements the Aware and BeanPostProcessor interfaces, both of which are related to the initialization of Spring beans. Therefore, we can infer that the main processing methods of this class come from the implementation methods of these two interfaces. This class also implements the order method.
That today, we take a look at AnnotationAwareAspectJAutoProxyCreator class calling process, in particular, Is to see ` ` AnnotationAwareAspectJAutoProxyCreator ` as BeanPostProcessor do what work, do what work as BeanFactoryAware.
Analysis AbstractAutoProxyCreator class
On AnnotationAwareAspectJAutoProxyCreator class inheritance relationships as you can see, Is in AbstractAutoProxyCreator class begin to implement SmartInstantiationAwareBeanPostProcessor interface and BeanFactoryAware interface.
So, let’s first take a look at the Abstractauto XyCreator class for analysis.
As defined by the AbstractautoxyCreator class, AbstractAutoProxyCreator class implements SmartInstantiationAwareBeanPostProcessor interface and BeanFactoryAware interface directly.
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
implements SmartInstantiationAwareBeanPostProcessor.BeanFactoryAware {
Copy the code
Since AbstractautoXyCreator implements the BeanFactoryAware interface, there must be a setBeanFactory() method in the AbstractautoxyCreator class, as shown below.
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
@Nullable
protected BeanFactory getBeanFactory(a) {
return this.beanFactory;
}
Copy the code
Sure enough, we found the setBeanFactory() and getBeanFactory() methods in the AbstractAutoProxyCreator class.
In addition, the AbstractautoxyCreator class also contains methods related to the BeanPostProcessor postprocessor, as follows: PostProcessBeforeInstantiation (), postProcessAfterInstantiation (), postProcessProperties (), postProcessBeforeInitialization (), postProcessAfterInitialization (). The overall source code is shown below.
@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) {returnwrapIfNecessary(bean, beanName, cacheKey); }}return bean;
}
Copy the code
That is, in the AbstractautoXyCreator class, there is logic for a postprocessor.
At this point, we see an implementation of BeanFactoryAware and a post-processor implementation in the AbstractautoXyCreator class.
Next, let’s take a look at AbstractAutoProxyCreator subclasses AbstractAdvisorAutoProxyCreator classes.
Analysis AbstractAdvisorAutoProxyCreator class
In AbstractAdvisorAutoProxyCreator class, we will see the following code.
@Override
public void setBeanFactory(BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
if(! (beanFactoryinstanceof ConfigurableListableBeanFactory)) {
throw new IllegalArgumentException(
"AdvisorAutoProxyCreator requires a ConfigurableListableBeanFactory: " + beanFactory);
}
initBeanFactory((ConfigurableListableBeanFactory) beanFactory);
}
Copy the code
Rewrite the setBeanFactory in AbstractAdvisorAutoProxyCreator class () method. And in AbstractAdvisorAutoProxyCreator setBeanFactory () method, the first call AbstractAutoProxyCreator setBeanFactory in class () method.
The initBeanFactory() method is called in the setBeanFactory() method, which is implemented as shown below.
protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
this.advisorRetrievalHelper = new BeanFactoryAdvisorRetrievalHelperAdapter(beanFactory);
}
Copy the code
The implementation of the initBeanFactory() method is relatively simple, and I won’t go into that here.
In addition, we have not found in the AbstractAdvisorAutoProxyCreator class method associated with post processor.
Next, we continue to analyze AbstractAdvisorAutoProxyCreator class a subclass of AspectJAwareAdvisorAutoProxyCreator class.
Analysis AspectJAwareAdvisorAutoProxyCreator class
By looking at the AspectJAwareAdvisorAutoProxyCreator source code of a class, we learn that, in AspectJAwareAdvisorAutoProxyCreator class no post processor related code. So, we continue up analysis AspectJAwareAdvisorAutoProxyCreator AnnotationAwareAspectJAutoProxyCreator subclass of a class.
Analysis AnnotationAwareAspectJAutoProxyCreator class
In AnnotationAwareAspectJAutoProxyCreator class, we can find a initBeanFactory () method, as shown below.
@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);
}
Copy the code
See here, you guys are a little bit clear about the call flow of setBeanFactory? SetBeanFactory () is called as follows: First in the class will perform AbstractAdvisorAutoProxyCreator setBeanFactory () method, SetBeanFactory in AbstractAdvisorAutoProxyCreator class in class () method will call his father AbstractAutoProxyCreator setBeanFactory () method, Then in AbstractAdvisorAutoProxyCreator class setBeanFactory initBeanFactory () method call () method. Because in the subclasses AnnotationAwareAspectJAutoProxyCreator rewrite the initBeanFactory () method, Final call is AnnotationAwareAspectJAutoProxyCreator initBeanFactory in class () method. That’s a little convoluted, but let’s look at a picture.
Note that in the picture above AbstractAdvisorAutoProxyCreator setBeanFactory in class () method as the entrance to the program calls, It will, in turn, calls AbstractAutoProxyCreator# setBeanFactory () and AnnotationAwareAspectJAutoProxyCreator# initBeanFactory (), and then, Again by AnnotationAwareAspectJAutoProxyCreator# initBeanFactory () call AbstractAdvisorAutoProxyCreator# initBeanFactory ().
In addition, we in the AnnotationAwareAspectJAutoProxyCreator class, did not find that post processor related code.
Ok, above is our analysis about AnnotationAwareAspectJAutoProxyCreator class source code. In the next article, we’ll start debugging the actual execution of the source code.
Ok, that’s enough for today, I’m Glacier, and we’ll see you next time