The premise to review
The previous article mainly introduced the IOC container mechanism and the core operation principle of spring’s core feature mechanism. Next, we will introduce another core function, namely the AOP container mechanism, which is responsible for the dynamic proxy mechanism of the previous article: With JDKProxy and CglibProxy behind us, we’ll look at how to implement the AOP container proxy mechanism.
AOP entry mechanism
How to implement Aspectj’s dynamic weave into Spring container beans?
The basic implementation principle of the implementation is the post-processor: BeanPostProcessor mechanism to achieve dynamic implantation mechanism.
How to implement Aspectj related weave in time
When the bean is initialized, it calls the corresponding BeanPostProcessor and the corresponding method is woven in.
The basic flow of judgment
Depends on the wrapIfNecessary method:
Determine that the current Bean is an AOP infrastructure type
If it is an infrastructure type, the bean object is returned directly back, without the associated initialization of the corresponding AspectJ dynamic weaving mechanism.
If it is a custom bean object type
An enhanced notification class that looks for when the Bean in question corresponds.
If the notification enhancement array collection for the object is not empty
The bean object will be enhanced with additional operations to generate the relevant proxy object and return the object after the execution, otherwise the object will be returned directly.
Filter notifier for when
GetAdvicesAndAdvisorsForBean method is the core of our Advice to enhance class screening method, it is mainly used for filtering and screening to bean when an intensifier array of information.
Find the notification enhancer for the corresponding Bean
Is mainly used for calling AnnotationAwareAspectJAutoProxyCreator findCandidateAdvisors * * * * () method, the interior will be the core of the first close to build relevant Aspectj class related implementation of the operation
Build the Aspectj class buildAspectJAdvisors method
- Firstly, the BeanName data set of all containers that are closed first is obtained
- Gets the associated BeanType collection from the BeanName name collection above the root
- Determine whether the current Bean belongs to an Aspectj annotation based on the BeanType. Class, if not, the class does nothing.
Build the actual related Advisors class mechanism
AdvisorsFactory. GetAdvisors get notification
Point of contact to deal with
The tangential class processing operations are not complete and then the actual execution of building dynamic proxy objects,
- Gets a list of methods in the Aspectj class other than the PointCut annotation obtained in the above operation.
- According to the corresponding Aspectj class and the related Advisors method list, create the related Advisor implementation class, which will internally traverse the related method list and call the corresponding method getAdvisor method to create the Advisor object.
- Create related AspectJExpressionPointCut object, and from the method in the annotations expression parsing, this last set to the corresponding layer facade Advisor object instances.
- Actual Advisor actual implementation class object: InstantitationModelAwarePoincutAdvisorImpl instance, and invoke its internal instantiateAdvice method build notification mechanism.
- The getAdvice method is still called internally, and the type of annotation that builds the relevant annotation creates the corresponding notification.
Filter the notifier when and execute the application
FindAdvisorsThatCanApply method
Extension related to screen out the notification list, extendAdvisors method, notifier list first to add a notifier DefaultPointcutAdivosr types, namely ExposeInvocationInterceptor. The implementation mechanism of ADVISOR.
Creating a proxy object
- JDK dynamic proxy
- Cglib dynamic proxy
proxy-target-class
The default value of the proxy-target-class attribute indicates whether the proxy implementation class can be supported. The default value of false indicates that the dynamic proxy mechanism of JDK is used to generate proxy objects when the bean has an implementation interface. If true, it indicates that the proxy object is generated using Cglib.
Such as:
<aop:aspectj-autoproxy proxy-target-class = "true" /></aop>
Copy the code
AopProxy interface
- CglibAopProxy interface implementation
- JdkDynamicAopProxy interface implementation
AOP proxy objects invoke the same kind of method problem solution
Expose – proxy
Only when the expose-proxy attribute is set to true can the corresponding proxy mechanism be exposed.
In order to solve the problem of the target method calling other methods in the same object, the aspect logic of the other methods cannot be implemented because the related this operation is involved instead of the proxy object mechanism.
You can implement a cast to the currentProxy object using aopcontext.currentproxy ().
Interceptor link execution
Intercept method mechanism
To obtain the corresponding method of the interceptor stack link related, if there is no access to the related cache link, may be called directly related getInterceptorsAndDynamicInterceptorAdvice obtain first close the interceptor chain.
Method interceptor associated interception operation join points
A pre-closed notifier of the PointcutAdvisor type is invoked, where the class and method are matched by a Pointcut held by the notifier concerned. The match overflows, indicating that the relevant notifier is weaving logical control to the current method. Non-methodintercptor notifications are also converted using the geIntercptors() method. Returns an array of associated interceptors and is then cached.
How a target method is executed
If the interceptor is empty
The call is executed directly through the reflection control of the proxy mechanism.
If not empty
, such as the call object and the jdkDynamicAutoProxy ReflectiveMethodInvocation objects, such as its process method to start the interceptor stack invoke method.
- Invoke: Executes the interceptor stack
- InvokeJoinpoin () : Executes the target method
Process the return value, and return the value.