Foreword
Each agent class can only serve as an interface, so that the program would have a lot of development in the proxy class.
So we will try to complete all of the proxy function through a proxy class, then we need to use dynamic proxies
In Java in order to achieve dynamic proxy mechanism, it is necessary java.lang.reflect.InvocationHandler
interfaces and java.lang.reflect.Proxy
support classes
java.lang.reflect.InvocationHandler
The interface is defined as follows:
//Object proxy:被代理的对象
//Method method:要调用的方法
//Object[] args:方法调用时所需要参数
public interface InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
}
java.lang.reflect.Proxy
Class is defined as follows:
//CLassLoader loader:类的加载器
//Class<?> interfaces:得到全部的接口
//InvocationHandler h:得到InvocationHandler接口的子类的实例
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
Dynamic proxy class can only proxy interfaces (abstract classes are not supported), the proxy class needs to implement InvocationHandler class, implement invoke methods. The invoke method returns a value needs to be called when the invoke method is to call all the methods are proxy interface is a proxy class that implements the interface