Do a qualified procedure analysis of the ape Spring AOP Source (15) analysis of the Jdkdynamicaopproxy invoke method

Source: Internet
Author: User

In the previous section, we have analyzed how proxyfactorybean is going to generate a proxy for a target object, and this section will discuss the source code of the core callback method invoke based on the JDK dynamic agent:


First open the Jdkdynamicaopproxy.java as follows



The Jdkdynamicaopproxy.java file is a 2 interface that implements the Aopproxy and Invocationhandler

First of all, Aopproxy this interface, the Aopproxy interface defines 2 methods


Let's look at the inheritance of this interface again.

Well, as a native JDK-based dynamic agent, the Jdkdynamicaopproxy has been perfectly returned to the target object's proxy, please see a section of the content in detail

http://blog.csdn.net/linuu/article/details/50972036


Re-analysis Invocationhandler This interface, this interface core method is the Invoke method


Invoke this method first defines a number of variables, temporarily do not see

There are three if in the red box:

The first if is to determine if the target object to be executed by the agent is equal executes the jdkdynamicaopproxy (that is, the proxy object's equal) method, and then returns, saying that spring does not do AOP interception of the equal method

The second if is to determine if the target object to be executed by the agent is Hashcode execute the jdkdynamicaopproxy (that is, the proxy object Hashcode) method, and then return, similarly, spring does not do AOP interception hashcode

The third if is to judge if the object of the agent itself is the implementation of the advised interface, do not do processing, direct execution, (spring means I do not do the aspect of the plane? )


Then take a look at the next core approach

If you look at the official English comments, you know that all you have to do now is get this method all the interceptors, form the intercept chain back, and enter the Getinterceptorsanddynamicinterceptionadvice method


Rows 479 and 4,802 are the interception chains that are looking for the method from the cache (which may have been called several times by a method of the proxy object, the first time that the call is fetched, and then taken directly from the cache, without having to get it multiple times, to improve performance), and if you have already obtained it, return directly


Well, this is the first time we're going to call, and then we'll see getinterceptorsanddynamicinterceptionadvice this way.

The ① section first defines the list size of an intercept chain to the maximum number of incoming advisors, and then see if our incoming advisor is also a subclass of the Introductionadvisor interface. Introductionadvisor This interface we have not analyzed, this should be based on the class of interceptors, can not intercept the specific method of the class, no pointcutadvisor flexible, we can understand it here.


Then the program to do is to loop each of our incoming advisor, and then strong into the Pointadvisor,② is the core of the current interception is to match the current method to execute the agent (in fact, is to determine whether the current advisor's pointcut is such a method), We said in the last verse that our break-in Pointcut is a "Magnum" Pointcut:



Let's see matches this way.

This is why this versatile pointcut can cut any method of the class (in fact, this is an interceptor filter, should we in the production environment, we generally use regular expression to define the tangent point (expression), because not every method needs to cut, will affect performance, So ② in matches this method is very important)


If it matches, put it in the interceptorlist defined in the method.


Let's go back to Jdkdynamicaopproxy's Invoke method, and then look at


At this time our interception chain is certainly not empty, direct analysis else,invocation is the first line in the Invoke method of the definition of methodinvocation, here invocation is actually to be all the parameters ready:

Parameter collation is the integration of our previous agent, target object, the name of the intercepted method, the parameters of the interception methods and the interceptor chain all into the Reflectivemethodinvocation class.


Crossing think about it, reflectivemethodinvocation this class has so many parameters, you can do whatever you want to do, first it can directly execute the target object of the method (class, name, parameter) can be executed (how to do?). Don't make a fuss, the name is clear, reflect!!!!. ), and with the interceptor chain, just know that the type of interceptor is front, back, surround type, it is OK The interceptor is also executed, so all things are ready ~


Finally, let's take a look at the process. Proceed () method


Well, let's take a look at how spring performs, first line look at the Interceptor chain, default from 1 execution (get (-1)? Wrong, the following ++this.currentinterceptorindex first, so that starting from the No. 0 to execute the Interceptor


Spring determines whether our facets need to be dynamically matched to the tangency point, and our side is a very common universal pointcut, so no, no matter what the pointcut is, the Invoke method is executed, and the incoming (reflectivemethodinvocation)

Because we're methodbeforeadviceinterceptor.



This is the before method that we implement in Methodinvokecountadvice, and Methodloggeradvice This.currentinterceptorindex


And then the recursive call, unlike the last time is this.currentinterceptorindex this + 1, so will execute the next interceptor, to the last will go:

The last point of execution is the method of our target object.


So far about the Proxyfactorybean basically said the end, or want to debug their own to see it ~






Do a qualified procedure analysis of the ape Spring AOP Source (15) analysis of the Jdkdynamicaopproxy invoke method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.