Spring5 source code analysis-------Aop article (1)

The @EnableAspectJAutoProxy annotation was mentioned in the previous article ( click to enter ). It has also been mentioned that its core is @Import(AspectJAutoProxyRegistrar.class), but this is also the most important class in our SpringAop. Let's look at the AOP core source code
Insert picture description here
annotations. Turn on SpringAop
Insert picture description here
and inject the SpringAOP aspect class into the IOC container.
Insert picture description here
Register the aspect class
Insert picture description here
Insert picture description here
AnnotationAwareAspectJAutoProxyCreator.class and register it in the container, and register the AOP entry
Insert picture description hereregistry.registerBeanDefinition("org.springframework.aop.config.internalAutoProxyCreator", beanDefinition);
Is it very familiar here? The first parameter is beanName, and the second parameter is the basic information of the bean. Let’s see that the basic information of beanDefinition is just encapsulated and loaded into RootBeanDefinition, and this object is also in front of us. As mentioned, it is a traditionally injected bean object, and then look ahead to see which object this cls is.
Insert picture description here
cls is this class, right? (Check the last position of the mouse ctrl+alt+←). Well, the collection of bean information is this class. Now let's look at this class. First, check the class diagram of AnnotationAwareAspectJAutoProxyCreator.
Insert picture description here
We will find that its final parent class is so familiar. BeanPostProcessor, did we talk about it before? Post processor! ! !
Okay, we are continuing to click on its parent class
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Arriving at the AbstractAutoProxyCreator class, this is the second important point, which is why I will circle it in the class diagram.
When we looked at the class diagram earlier, we also knew that AbstractAutoProxyCreator was a subclass of BeanPostProcessor. Then he must have implemented front and rear processors. We look at all the methods of this class (Alt+7) and
Insert picture description here
we will see two pairs of pre- and post-processors. So which pair will it be? We went back to the source code when we looked at the bean cycle before and found their pre- and post-processing.
(The search method is omitted, you can refer to the article https://blog.csdn.net/weixin_43911969/article/details/114003861).
Insert picture description here
Found their front and rear processors, let's click in and take a look.
Respectively click to enter the front and rear processor
front:
Insert picture description here
Insert picture description here
Insert picture description here
Now we can see clearly, we are taking a method like public Object postProcessBeforeInitialization(Object bean, String beanName)

The post
Insert picture description here
-processor is the same as the choice of the pre-processor, it is also AbstractAutoProxyCreator. After entering it
Insert picture description here
, we will find that the pre-processor does not perform any logical processing, but the logical processing by the post-processor. Then we start with the post processor. Here, as a reminder, you will find that the class we came in from the life cycle of the bean happens to be the class that we had previously had two pairs of pre- and post-processors.
Let's think about a question. As mentioned earlier, dynamic proxy, then which dynamic proxy does Aop use?
Starting from the post processor (after all, the preprocessor does not perform logical processing)
check this method (the wrapIfNecessary() method judges whether the object is within the scope of the AOP entry point scan package.)
Insert picture description here
Look at this part of the code
Insert picture description here
protected static final Object[] DO_NOT_PROXY = null;
if (specificInterceptors != DO_NOT_PROXY) Determine whether enhancements are made (whether notifications are used)
Object proxy = this.createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); if enhancements are used , Just create an agent. Okay, we click into it to know which form
Insert picture description here
to use to obtain the agent when creating the agent.
Insert picture description here
Create a proxy method.
Insert picture description here
Insert picture description here
So far we have seen that there are cglib and jdk agents, so Aop both agents are used. But there is still judgment in it.
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config))
//config.isOptimize() Whether to use strategy optimization for the generation of proxy classes. Its function is the same as isProxyTargetClass. The default is false
// config.isProxyTargetClass() Whether to use Cglib to create proxy objects The default is false
//hasNoUserSuppliedProxyInterfaces Whether there is an interface in the target class and there is only one interface when the interface type is not SpringProxy type

if (targetClass.isInterface() || Proxy.isProxyClass(targetClass))
// Determine if it is an interface or a proxy class, use JDK dynamic proxy

Guess you like

Origin blog.csdn.net/weixin_43911969/article/details/114081856