This article explores what Spring AOP is based on the 13th chapter of deep Analysis of the insider of Java Web technology
In short, it's cut-face programming. The implementation of Spring AOP is implemented by the dynamic proxy of the JDK used by the interface, and is implemented by the proxy of the class using Cglib. JDK Dynamic Proxy
JDK Dynamic agent, is in the process of running the program, according to the interface of the agent to dynamically generate proxy class file, and load the running process. The purpose of the proxy is to invoke the Invocationhandler invoke method when invoking the target method, in fact Spring AOP is also here to make a fuss. It's also a typical proxy model.
The following simple code illustrates this problem to achieve its own invocationhandler
/**
* *
* @author Yvan * */Public
class Aopproxy implements Invocationhandler {
private Object Realobject;
Public Aopproxy (Object realobject) {
super ();
This.realobject = Realobject;
}
@Override Public
object Invoke (Object proxy, Method method, object[] args) throws Throwable {
System.out.println ("Do something before execute Invoke method ...");
Object invokeresult= Method.invoke (realobject, args);
System.out.println ("Do something execute Invoke method ...");
Return Invokeresult
}
}
To develop interfaces and their implementation classes
/**
* *
* @author Yvan
*/public
interface subobject {public
string execute (String ... param);
}
/**
* *
* @author Yvan * */Public
class Realobject implements Subobject {
@Override
public string Execute (string ... param) {for
(int i = 0; i < param.length; i++) {
System.out.println (param[i) );
}
Return ' Execute method ';
}
Testing dynamic Agents
/**
* *
@author Yvan
* * /Public
class Appmain {public
static void Main (string[] args) {
Subobject realobject = new Realobject ();
Aopproxy aopproxy = new Aopproxy (realobject);
Proxy generation Agent
subobject subobject = (subobject) proxy.newproxyinstance (Aopproxy.getclass ()) provided through the JDK. getClassLoader (), Realobject.getclass (), getinterfaces ()
, aopproxy);
System.out.println (Subobject.getclass (). GetName ());
System.out.println (subobject instanceof Proxy);
String result = Subobject.execute ("parameter One", "parameter Two");
SYSTEM.OUT.PRINTLN (result);
}
Print results
Com.sun.proxy. $Proxy 0
True do
something before execute Invoke method ...
Parameter
Two does
something after execute Invoke method ...
Execute method
To highlight: See Com.sun.proxy. $Proxy 0 is a dynamically generated proxy class for JDK, subobject instanceof Proxy is printed as ture stating that the proxy class inherits proxy.
Here is a question: Why JDK dynamic proxies must be based on interfaces
The reasons are as follows:
1, the generation of proxy class inherited proxy, because Java is a single inheritance, so can only implement interface, through the interface to achieve
2, from the design of Agent mode, make full use of the Java polymorphic characteristics, but also in line with the interface code based on the specification
Of course, the JDK also explains in the parameters of the build agent that a corresponding interface needs to be passed in
public static Object newproxyinstance (ClassLoader loader,
class<?>[] interfaces,
Invocationhandler h)
throws IllegalArgumentException
Cglib Agent
Cglib bottom: Use bytecode to process the framework ASM to convert bytecode and generate new classes .
The Cglib (CODE generlize LIBRARY) proxy implements a proxy for a class, primarily by generating a subclass of the specified class, overwriting all of its methods, so that the class or method cannot declare final.
If the target object does not implement an interface, the Cglib proxy is used by default;
If the target object implements an interface, you can force the use of Cglib to implement the proxy.
Add the Cglib library and add <aop:aspectj-autoproxy proxy-target-class= "true" in the spring configuration/>