Proxy.newProxyInstance中,invoke第一个参数用target,为什么不能用this关键字?
第一个参数就是你要代理的对象,this是你调用时所处的类,这两者压根就不是一回事
InvocationHandler可不是真实运行时接口的代理对象,jdk会自己生成一个类并且实现你要代理的接口,并且将InvocationHandler嵌入到实现类中,实现类调用InvocationHandler的时候,会把实现类自己当作参数传入进去,你在InvocationHandler里面用的this则是InvocationHandler它自己,这可不是一个东西
InvocationHandler is the interface implemented by the invocation handler of a proxy instance.
调用处理器是一个通过 代理对象的调用处理器(Handler实现类中的invoke) 来实现的接口
Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.
每一个代理对象都有相关联的调用处理器。 当一个方法伴随所属代理对象被调用,这个方法调用就会被编码转移为调用处理器中的相应扩展方法调用。
总的来讲,每一个proxy代理对象都有一个实现InvocationHandler接口的实现类Handler,这个实现类中的invoke方法就是proxy代理对象的实际调用处理器,在这个invoke方法体中,有所有 被代理对象 的方法逻辑实现和扩展。
每当代理对象proxy被反射机制用于调用其方法时,这个方法调用就自动转移为Handler类里invoke方法体中的对应扩展方法调用。
InvocationHandler这个接口的唯一一个方法 invoke 方法:
Object invoke(Object proxy, Method method, Object[] args) throws Throwable
proxy:代理对象,只是反射机制调用方法的需要
method: proxy被反射机制用于调用的方法对象
args:调用方法的参数列表