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
annotations. Turn on SpringAop
and inject the SpringAOP aspect class into the IOC container.
Register the aspect class
AnnotationAwareAspectJAutoProxyCreator.class and register it in the container, and register the AOP entry
registry.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.
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.
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
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
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).
Found their front and rear processors, let's click in and take a look.
Respectively click to enter the front and rear processor
front:
Now we can see clearly, we are taking a method like public Object postProcessBeforeInitialization(Object bean, String beanName)
The post
-processor is the same as the choice of the pre-processor, it is also AbstractAutoProxyCreator. After entering it
, 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.)
Look at this part of the code
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
to use to obtain the agent when creating the agent.
Create a proxy method.
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