1, Java comes with the proxy class can create dynamic classes, if a class implemented an interface so long can create a proxy for this class.
2, Agent: is when the user to call a method of a class, the user can call the proxy, proxy through the interface call the original method of the class, the agent before the method to the user can add some methods, such as error log, the user class method run time to listen to the performance of the class method. The Invoke method in Invocationhandler is initiated when the proxy is completed when the proxy invokes the method. The user does not know which class to use for the user, so in the framework with the configuration file to obtain the proxy class, the user needs to use the framework to modify the configuration file.
public class Proxydemo {public static void main (string[] args) throws Exception {//This is divided into 2 parts to implement the collection proxy, the following is a 1-step implementation of the proxy, and is any class//class Classproxy=proxy.getproxyclass (Collection.class.getClassLoader (), collection.class);//constructor[ ]constructors=classproxy.getconstructors ();//Constructor Constructor=classproxy.getconstructor ( Java.lang.reflect.InvocationHandler.class)//for (Constructor c:constructors)//{//System.out.println (C.tostring ( );//}////final ArrayList a=new arraylist<> ();//collection c= (Collection) constructors[0].newinstance (new Invocationhandler () {////@Override//public object Invoke (Object proxy, method, object[] args)//throws Throwable { ArrayList a=new arraylist<> ();//system.out.println (System.currenttimemillis ());//object Ob=method.invoke ( A, args);//system.out.println ("a Size:" +a.size ());//system.out.println (System.currenttimemillis ());//return ob;/ /}////});////c.add ("Dada"); C.add ("Dada");////system.out.println (C.size ());/////////////The following is the principle of implementation of the agent, PR is the interface to the agent, Advice is the interface of the agent to monitor method final Pro pr=new protextest (); final Advice advice=new myadvice (); Pro classproxy= (PRO) Proxy.newproxyinstance (Pr.getclass () getClassLoader (), New Class[]{pro.class}, new Invocationhandler () {@Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable { Advice.beforemethod ();//This is the method that is called before the method object O=method.invoke (pr, args); Advice.aftermethod ();//This is the method that is called after Return o;}); /When calling SayHello, the system will look for the Invoke method in the proxy call Invocationhandler. Classproxy.sayhello ();/* So the print result is: 1441522392491hello1441522392493*///Call agent function Pro o= (PRO) Daili (Pr,advice); O.sayhello (); }/******************. The above method is extracted as a proxy method, for any class proxy implementing the interface *****//* * target for the target class implementation interface * Advice for the agent to monitor the interface of the method */public static Object Daili (Final Object target,final Advice Advice) {object Classproxy= (object) proxy.newproxyinstance (TARGET.GETCLA SS (). getClassLoader (), Target.getclass (). Getinterfaces (), new Invocationhandler () {@Overridepublic Object invoke ( Object Proxy, method, object[] args) tHrows throwable {advice.beforemethod ();//This is the method called before method object O=method.invoke (target, args); Advice.aftermethod ();// This is the method that is called after return o;//returns the value returned after the method is executed}); return classproxy; }}
Java Proxy Learning, through the class implementation interface to implement the proxy. Proxy to create the dynamic class, and the implementation of the Invocationhandler interface, and how it works.