1. Interface
Public Interface Hello { publicvoid SayHello ();}
2. Instance Classes
Public class Hello2 { publicvoid SayHello () { System.out.println ("Hello world2! " ); }}
Public class extends hello2{ }
Public class Implements hello{ @Override publicvoid SayHello () { System.out.println (" Hello world! " ); }}
3.JDK Dynamic Agent
UblicclassJdktestImplementsinvocationhandler{PrivateObject object; @SuppressWarnings ("Unchecked") Public<T>T bind (Object obj) { This. Object =obj; returnT Proxy.newproxyinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), This); } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Before SayHello"); Method.invoke (Object,NULL); System.out.println ("After SayHello"); return NULL; } Public Static voidMain (string[] args) {jdktest test=Newjdktest (); Hello Hello= Test.bind (NewHelloimpl ()); Hello.sayhello (); }}
4.cglib Dynamic Agent
Public classCglibtestImplementsmethodinterceptor{PrivateObject obj; /*** Common Interface class proxy *@authorTomsnail * @date April 2, 2015 morning 10:36:10*/ Public<T>T instance (t obj) { This. obj =obj; Enhancer Enhancer=Newenhancer (); Enhancer.setsuperclass ( This. Obj.getclass ()); Enhancer.setcallback ( This); return(T) enhancer.create (); } @Override PublicObject Intercept (object arg0, Method arg1, object[] arg2, methodproxy arg3)throwsthrowable {System.out.println ("Before SayHello"); Arg3.invoke (obj, arg2); System.out.println ("After SayHello"); return NULL; } /*** No interface class proxy *@authorTomsnail * @date April 2, 2015 morning 10:35:58*/ Public<T>t Instanceobject (t obj) {T T= (T) enhancer.create (Obj.getclass (),NewMethodinterceptor () {@Override PublicObject Intercept (object arg0, Method arg1, object[] arg2, methodproxy arg3)throwsthrowable {System.out.println ("Hello2 Proxy"); returnArg3.invokesuper (arg0, arg2); } }); returnT; } /*** No interface class proxy *@authorTomsnail * @date April 2, 2015 morning 10:35:58*/ Public<T>t Instancesuperobject (t obj) {T T= (T) enhancer.create (Obj.getclass (), noop.instance);//when there is no proxy, call the parent class method to implement returnT; } Public Static voidMain (string[] args) {cglibtest test=Newcglibtest (); Hello Hello= Test.instance (NewHelloimpl ()); Hello.sayhello (); Hello2 Hello2= Test.instanceobject (NewHello2 ());//dynamic proxy with no interface classHello2.sayhello (); Hello2 Hello3= Test.instancesuperobject (NewHello3 ());//subclass does not override parent class methodthe dynamic agentHello3.sayhello (); }}
5. A little summary
The dynamic agent of the JDK can only be processed through the interface, which is difficult to handle if there is no interface. Cglib does not have this limitation.
There is performance, I saw an article on the internet has been compared, in JDK7/8 and Cglib, but the performance of the JDK is very good, see http://www.cnblogs.com/haiq/p/4304615.html
Re-learning Java Fundamentals (iii): Dynamic agents