Define interfaces: Interfaces are Bridges

package com.dynamic.proxy;

public interface ProxyInterface {
    public void sayHello();
    public void sayBye();
}
Copy the code

Define proxyed classes: Proxyed classes are methods that will be enhanced

public class ProxyInterfaceImpl implements ProxyInterface { @Override public void sayHello() { System.out.println("Hello!" ); } @Override public void sayBye(){ System.out.println("Good Bye!" ); }}Copy the code

Define handler:

package com.dynamic.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; Public class MyProxyInvocationHandler implements InvocationHandler {/** * private Object ProXied = null; /** * constructor, when creating MyInvocationHandler object, Public MyProxyInvocationHandler(Object Proxied){this.proxied = proxied; } /** * All implementation class object methods of the proxy interface when called, Override public Object invoke(Object proxy, Method Method, Object[] args) throws Throwable {/** * invoke = null; If (method.getName().equals("sayHello")){sayHello system.out.println (" preprocessing: Sally... ); rtInvoke = method.invoke(proxied, args); System.out.println(" postprocessing: 3Q....") ); } return rtInvoke; }}Copy the code

Test dynamic proxy functionality:

package com.dynamic.proxy; import com.dynamic.proxy.impl.ProxyInterfaceImpl; import org.junit.Test; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Proxy; public class ProxyClient { @Test public void test() { Thread t1 = new Thread(new Runnable() { @Override public void Run () {system.out.println ("------ first -------"); run() {system.out.println ("------ first -------"); $Proxy0 = $Proxy0; $Proxy0 = $Proxy0; Can not write System. The getProperties () put (" sun. Misc. ProxyGenerator. SaveGeneratedFiles ", "true"); / / (2) the load dynamic Proxy classes (array to be agent interface, that is, to the agent which interface) Class proxyClass = Proxy. GetProxyClass (ProxyInterface. Class. GetClassLoader (), ProxyInterface.class); // Select * from Proxy class; // Select * from Proxy class; The parameter Constructor is Proxy(InvocationHandler), so give the Constructor a Constructor of type // InvocationHandler. Class Constructor CST = null; try { cst = proxyClass.getConstructor(InvocationHandler.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } // (1) pass a custom InvocationHandler object to the constructor. MyInvocationHandler is passed into the constructor of the proxy class to complete the creation of the proxy object. // Finally, the proxy object is forced to interface type. // Why can be forced to ???? Because the JVM generated Proxy object (decompile javAP view) implements the Proxy interface // In addition, the generated Proxy class inherits the Proxy class and calls the invoke(,) method, This method is MyInvocationHandler // our custom InvocationHandler, ProxyInterfaceImpl = new ProxyInterfaceImpl(); ProxyInterfaceImpl = new ProxyInterfaceImpl(); MyProxyInvocationHandler myProxyInvocationHandler = new MyProxyInvocationHandler(proxyInterfaceimpl); ProxyInterface proxyintfs = null; try { proxyintfs = (ProxyInterface) cst.newInstance(myProxyInvocationHandler); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } proxyintfs.sayHello(); }}); t1.start(); try { t1.join(); }catch (InterruptedException e){ e.printStackTrace(); } Thread t2 = new Thread(new Runnable() {@override public void run() {** * Direct call the Proxy class static methods * / System. Out. The println (" -- -- -- -- -- - the second -- -- -- -- -- -- -- "); // The interface to be propped is passed in as an array type, different from the mutable arguments defined in the first case above (i.e.... ProxyInterface proxyIntfs = (ProxyInterface) Proxy.newProxyInstance(ProxyInterface.class.getClassLoader(), new Class[] {ProxyInterface.class}, new MyProxyInvocationHandler(new ProxyInterfaceImpl())); proxyIntfs.sayHello(); }}); t2.start(); try { t2.join(); }catch (InterruptedException e){ e.printStackTrace(); }}}Copy the code