What are the two implementation mechanisms of Spring AOP. Spring <iframe id= "iframeu788097_0" src= "http://pos.baidu.com/acom?rdid=788097&dc=2&di=u788097 &dri=0&dis=0&dai=1&ps=247x1206&dcb=baidu_union_define&dtm= Baidu_dup_setjsonadslot&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr= 1454246761341&ti=spring%20aop%e4%b8%a4%e7%a7%8d%e5%ae%9e%e7%8e%b0%e6%9c%ba%e5%88%b6%e6%98%af%e4%bb%80% E4%b9%88%ef%bc%9f%20%e2%80%94%e2%80%94%20it%e5%85%ac%e5%8f%b8%e9%9d%a2%e8%af%95%e6%89%8b%e5%86%8c&ari= 1&dbv=2&drs=1&pcs=1920x923&pss=1920x273&cfv=0&cpl=27& chi=1&cce=true&cec=utf-8&tlm=1396013260&ltu=http%3a%2f%2fwww.mianwww.com% 2fhtml%2f2012%2f11%2f17084.html&ltr=http%3a%2f%2fwww.baidu.com%2flink%3furl% 3d0wxuem5wzzhccbsyoiv6bgm6wubqxfrpnuufr_bsxwbc3v5khhksz98owf73qgvprpqmv4139qukqr_g7wu3da%26wd%3d%26eqid% 3df0c37c5c0002a4660000000456ae0b40&ecd=1&psr=1920x1080&par=1920x1040&pis=-1x-1&ccd=24&cja=true& amp;cmi=53&col=zh-cn&cdo=-1&tcn=1454246761&qn=0658fd932312088a&tt= 1454246761306.46.170.173 "width=" 336 "height=" 280 "align=" Center,center "vspace=" 0 "hspace=" 0 "marginwidth=" 0 " marginheight= "0" scrolling= "no" frameborder= "0" allowtransparency= "true" style= "border-width:0px; border-style:initial; Vertical-align:bottom; margin:0px; " ></iframe>
Spring is a dynamic proxy for AOP, and spring provides 2 implementation mechanisms
1. If the class that has an interface declaration is aop,spring called by the Java.lang.reflection.Proxy class to do the processing
Org.springframework.aop.framework.JdkDynamicAopProxy
Public Object GetProxy (ClassLoader ClassLoader) {
if (logger.isdebugenabled ()) {
Class Targetclass = This.advised.getTargetSource (). Gettargetclass ();
Logger.debug ("Creating JDK Dynamic Proxy" +
(Targetclass!= null?) "For [" + targetclass.getname () + "]": ""));
}
class[] proxiedinterfaces = aopproxyutils.completeproxiedinterfaces (this.advised);
Return Proxy.newproxyinstance (ClassLoader, proxiedinterfaces, this);
}
Org.springframework.aop.framework.ReflectiveMethodInvocation
Public Object Proceed () throws Throwable {
We start with a index of-1 and increment early.
if (This.currentinterceptorindex = = This.interceptorsAndDynamicMethodMatchers.size () –1) {
return Invokejoinpoint ();
}
Object Interceptororinterceptionadvice =
This.interceptorsAndDynamicMethodMatchers.get (++this.currentinterceptorindex);
if (Interceptororinterceptionadvice instanceof Interceptoranddynamicmethodmatcher) {
Evaluate Dynamic Method Matcher here:static part would already have
been evaluated and found to match.
Interceptoranddynamicmethodmatcher DM =
(Interceptoranddynamicmethodmatcher) Interceptororinterceptionadvice;
if (Dm.methodMatcher.matches (This.method, This.targetclass, this.arguments)) {
Return Dm.interceptor.invoke (this);
}
else {
Dynamic matching failed.
Skip This interceptor and invoke the next in the chain.
return proceed ();
}
}
else {
It's an interceptor and so we just invoke it:the pointcut'll have
been evaluated statically before this object is constructed.
Return ((Methodinterceptor) interceptororinterceptionadvice). Invoke (this);
}
}
2. What if there is a class without an interface declaration? Spring is implemented by Cglib packages and inner classes
private static class Staticunadvisedinterceptor implements Methodinterceptor, Serializable {
Private final Object target;
Public Staticunadvisedinterceptor (Object target) {
This.target = target;
}
public object Intercept (object proxy, Method method, object[] args,
Methodproxy methodproxy) throws Throwable {
Object RetVal = Methodproxy.invoke (target, args);
Return massagereturntypeifnecessary (proxy, Target, retVal);
}
}
/**
* Method Interceptor used for static targets and no advice chain, when the
* The proxy is to be exposed.
*/
private static class Staticunadvisedexposedinterceptor implements Methodinterceptor, Serializable {
Private final Object target;
Public Staticunadvisedexposedinterceptor (Object target) {
This.target = target;
}
public object Intercept (object proxy, Method method, object[] args, Methodproxy methodproxy) throws Throwable {
Object oldproxy = null;
try {
Oldproxy = Aopcontext.setcurrentproxy (proxy);
Object RetVal = Methodproxy.invoke (target, args);
Return massagereturntypeifnecessary (proxy, Target, retVal);
}
finally {
Aopcontext.setcurrentproxy (Oldproxy);
}
}
}
/**
* Interceptor used to invoke a dynamic target without creating a method
* Invocation or evaluating an advice chain. (We know there was no advice
* for this method.
*/
Private class Dynamicunadvisedinterceptor implements Methodinterceptor, Serializable {
public object Intercept (object proxy, Method method, object[] args, Methodproxy methodproxy) throws Throwable {
Object target = Advised.gettargetsource (). Gettarget ();
try {
Object RetVal = Methodproxy.invoke (target, args);
Return massagereturntypeifnecessary (proxy, Target, retVal);
}
finally {
Advised.gettargetsource (). Releasetarget (target);
}
}
}
/**
* Interceptor for unadvised dynamic targets the proxy needs exposing.
*/
Private class Dynamicunadvisedexposedinterceptor implements Methodinterceptor, Serializable {
public object Intercept (object proxy, Method method, object[] args, Methodproxy methodproxy) throws Throwable {
Object oldproxy = null;
Object target = Advised.gettargetsource (). Gettarget ();
try {
Oldproxy = Aopcontext.setcurrentproxy (proxy);
Object RetVal = Methodproxy.invoke (target, args);
Return massagereturntypeifnecessary (proxy, Target, retVal);
}
finally {
Aopcontext.setcurrentproxy (Oldproxy);
Advised.gettargetsource (). Releasetarget (target);
}
}
}
We can try it ourselves.
1.jdk Proxy method
First, an interface.
Ihelloworld.java
Package kris.aop.test;
Public interface Ihelloworld {
public void print (String name);
public void Write (String sth);
}
One more implementation.
Helloworld.java
Package kris.aop.test;
public class HelloWorld implements Ihelloworld {
public void print (String name) {
System.out.println ("HelloWorld" +name);
}
public void Write (String sth) {
System.out.println ("write" +sth);
}
}
proxy class
Defaultinvocationhandler.java
Package kris.aop.test;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
public class Defaultinvocationhandler implements Invocationhandler {
/**
* Replace the method called by the external class
* An instance of Invocationhandler has already been wrapped outside obj
* Method External methods
* Args Method parameters
*/
public object invoke (Object obj, Method method, object[] args)
Throws Throwable {
String S1 []={"Kris"};
String S2 []={"Anyone"};
Ihelloworld ihw=new HelloWorld ();
System.out.println ("start!");
Method.invoke (Ihw,args);
Method.invoke (IHW,S1);
Object O=method.invoke (IHW,S2);
System.out.println ("stop!");
return o;
}
}
Test class
Test.java
Package kris.aop.test;
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Proxy;
public class Test {
public static void Main (String args []) {
Class clazz = new HelloWorld (). GetClass ();
ClassLoader cl = Clazz.getclassloader ();
Class classes [] = Clazz.getinterfaces ();
Invocationhandler ih=new Defaultinvocationhandler ();
Use Invocationhandler to give HelloWorld an AOP wrapper
Ihelloworld ihw= (Ihelloworld) proxy.newproxyinstance (CL,CLASSES,IH);
Ihw.print ("test");
Ihw.write ("test");
}
}
2. Implement with Cglib package, first don't forget to introduce the package
Package kris.aop.cglib.test;
public class HelloWorld {
public void print (String name) {
System.out.println ("HelloWorld" +name);
}
public void Write (String sth) {
System.out.println ("write" +sth);
}
public void print () {
System.out.println ("HelloWorld");
}
}
proxy Class (No inner class, clear dot)
Package kris.aop.cglib.test;
Import Java.lang.reflect.Method;
Import Net.sf.cglib.proxy.MethodInterceptor;
Import Net.sf.cglib.proxy.MethodProxy;
public class Methodinterceptorimpl implements Methodinterceptor {
public object intercept (object obj, Method method, object[] args,
Methodproxy proxy) throws Throwable {
System.out.println (method);
Proxy.invokesuper (obj, args);
return null;
}
}
Test class
Package kris.aop.cglib.test;
Import Net.sf.cglib.proxy.Enhancer;
public class Test {
public static void Main (string[] args) {
Enhancer enhancer = new enhancer ();
Enhancer.setsuperclass (Helloworld.class);
Set callback method implementation class
Enhancer.setcallback (New Methodinterceptorimpl ());
Instantiating a HelloWorld instance that has added a callback implementation
HelloWorld my = (HelloWorld) enhancer.create ();
My.print ();
}
}