Concept:
Static proxy: The source code is generated automatically by the programmer or a specific tool, and then compiled. Before the program runs, the. class file for the proxy classes already exists.
Dynamic Agent: When the program is running, it is created dynamically using the reflection mechanism.
The dynamic proxy of the JDK is very simple to use, when it has a limitation that objects that use dynamic proxies must implement one or more interfaces. What if you want to delegate an inherited class that does not implement an interface ? Now we can use the Cglib package.
JDK Dynamic Proxy implementation
import java.lang.reflect.constructor;import java.lang.reflect.invocationhandler;import java.lang.reflect.invocationtargetexception;import java.lang.reflect.method;import Java.lang.reflect.proxy;import java.util.arraylist;import java.util.collection;public class proxytest { static stringbuilder sbuilder=new stringbuilder () ; public static void main (String[] args) throws nosuchmethodexception, securityexception, instantiationexception, illegalaccessexception, illegalargumentexception, invocationtargetexception { //proxy class byte code class clazzproxy1= Proxy.getproxyclass (Collection.class.getClassLoader (), collection.class);//proxy class construction method list, There is no parameterless construction method System.out.println (".... begin constructors list.... "); constructor[] constructors= clazzproxy1.getconstructors (;); for (constructor constructor:constructors) { stringbuilder sbuilder=new stringbuilder (); sbuilder.append (Constructor.getname ()). Append ("("); class[] clazzparams =constructor.getparametertypes (); for (Class clazzparam:clazzparams) { sbuilder.append (Clazzparam.getname ()). Append (","); } &nbSp; if (clazzparams.length!=0) { sbuilder.deletecharat ( Sbuilder.length ()-1); } sbuilder.append (")"); system.out.println ( Sbuilder.tostring ()); } //proxy class method list system.out.println ("..... begin methods list .............. method[] methods= clazzproxy1.getmethods (); for (method method:methods) { Stringbuilder sbuilder=new stringbuilder (); Sbuilder.append (Method.getname ()). Append ("("); class[] clazzparams =method.getparametertypes (); for (Class clazzparam:clazzparams) { sbuilder.append (Clazzparam.getname ()). Append (","); } if (clazzparams.length!=0) { sbuilder.deletecharat (Sbuilder.length ()-1); } sbuilder.append (")"); &nbSp; system.out.println ( sbuilder.tostring ()); } //creates an instance of the proxy class through bytecode, and cannot be passed into the invocationhandler using newinstance (). system.out.println ("..... begin create instance object .............. constructor constructor= Clazzproxy1.getconstructor (Invocationhandler.class); Collection proxy1= (Collection) constructor.newinstance (new invocationhandler () { @Override public object invoke (Object proxy, method method, object[] args) throws Throwable { return null; } }); system.out.println (Proxy1); proxy1.clear ();//method with no return value succeeded // One step create proxy class instance collection proxy2= (Collection) proxy.newproxyinstance ( Collection.class.getClassLoader (), New class[]{collection.class}, new invocationhandler () { ArrayList target=new ArrayList<> ();//proxy object, target object @Override public object invoke (Object Proxy, method method, object[] args) throws Throwable { Long begintime=system.currenttimemillis (); object retval=method.invoke (Target, args); long endtime=system.currenttimemillis (); system.out.println (Method.getname () + " running time is " + (endtime-begintime)); Return retval; } }); proxy2.add ("Lhm"); proxy2.add ("zxx"); proxy2.add ("BxD"); system.out.println (Proxy2.size ()); system.out.println (Proxy2.getclass (). GetName ()); } }
This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1719600
Java dynamic Proxy implementation proxy and Invocationhandler