Each dynamic proxy class must implement the Invocationhandler interface, and each instance of the proxy class is associated with a handler, and when we call a method through the proxy, The invocation of this method is forwarded to the Invoke method of this interface by Invocationhandler.
/** * * @param proxy Agent's real objects * @param method method object that invokes the real object of one of the methods * @param args Call method Simultaneous parameters * @return Call result * @ throws throwable */@Overridepublic object invoke (object proxy, method Method, object[] args) throws throwable {}
Proxy: The dynamic creation of a proxy object class, we use more is newproxyinstance this method.
/** * * @param loader ClassLoader object, which ClassLoader object to load the generated proxy object * @param interfaces Interface Object array, providing a set of interfaces to it, then this agent The elephant claims to have implemented the interface (polymorphic), which we can call the method in this set of interfaces * @param h Invocationhandler object, when the dynamic proxy object is called on the method, which Invocationhandler object will be associated with the * * @CallerSensitivepublic static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h) throws IllegalArgumentException {}
Demo
Subject interface
public interface Subject {void say (String str);}
Realsubject class
public class Realsubject implements Subject {@Override public void say (String str) {System.out.println ("hel Lo, "+ str"); }}
Dynamic proxy Classes
public class dynamicproxy implements invocationhandler { // Real objects to proxy private Object target; // Assigning an initial value to an agent's real object public dynamicproxy (object target) { this.target = target; } @Override public object invoke (Object proxy, method method, object[] args) throws Throwable { Before (); // when a contemporary object invokes a method of a real object, It automatically jumps to the proxy object associated with the handler object's Invoke method to invoke method.invoke (target, args); after (); return null; } &nbsP; public void before () { System.out.println ("before ..."); } public void after () { system.out.println ("After ..."); }}Client class
public class Client {public static void main (string[] args) {Subject realsubject = new Realsubject (); The real object of the proxy is passed in, and finally the Invocationhandler handler = new Dynamicproxy (Realsubject) that invokes its method through the real object; Create objects by Proxy Subject Subject = (Subject) proxy.newproxyinstance (Handler.getclass (). getClassLoader (), Realsubject . GetClass (). Getinterfaces (), handler); System.out.println (Subject.getclass (). GetName ()); Subject.say ("Webb"); }}Output results
Com.sun.proxy. $Proxy 0before...hello, Webbafter ...
We see Subject.getclass (). GetName () to see that the print is $Proxy 0, first explain why it can be converted to an object of type subject? The reason is that in newproxyinstance the second parameter of this method, we give this proxy object a set of what interface, then this proxy object will implement this kind of interface, this time we can convert this proxy object coercion type to any of this set of interfaces , because the interface here is the subject type, so it can be converted to the subject type. At the same time we must remember that the proxy object created through Proxy.newproxyinstance is an object that is dynamically generated when the JVM is running, it is not our invocationhandler type, nor is it the type of the set of interfaces we define. Instead, the run is a dynamically generated object, and the naming method is in this form, starting with $, proxy is medium, and the last number represents the label of the object.
When we execute Subject.say ("Webb"), it automatically jumps to the Invoke method in the handler associated with the proxy object, and the handler object accepts a parameter of type Realsubject, This is the real object that I want to proxy, so I call the Invoke method in handler to execute it.