InvocationHandler的invoke方法的第一个参数问题

本文探讨了Java动态代理机制中InvocationHandler接口的invoke方法使用细节。解释了该方法的第一个参数实际上是代理类实例而非被代理类。通过示例代码及反编译过程,清晰展示了代理类如何调用invoke方法。

今天有同事问起,动态代理中的InvocationHandler的invoke方法的第一个参数。是代理类还是委托类(即被代理类). 其实这个参数是代理类。我们看一下生成的代理类就会明白:

public interface ProxyInf {
   public void say();
}
public class TestProxy{
    public void say() {
       System.out.println("nothing");
    }
                      
    public static void main(String args[]) throws IOException{
         byte[] generateProxyClass = ProxyGenerator.generateProxyClass( 
                 "ProxyImpl", new Class<?>[] { ProxyInf.class}); 
         FileOutputStream fos = new FileOutputStream("c:\\ProxyImpl.class"); 
         fos.write(generateProxyClass); 
         fos.close(); 
    }
}

反编译C盘下面的ProxyImpl.class类文件,你会发现:

public final class ProxyImpl extends Proxy
  implements ProxyInf
{
  private static Method m3;
  private static Method m1;
  private static Method m0;
  private static Method m2;
  public ProxyImpl(InvocationHandler paramInvocationHandler)
    throws {
    super(paramInvocationHandler);
  }
  public final void say()
    throws {
    try {
      this.h.invoke(this, m3, null);
      return;
    }
    catch (RuntimeException localRuntimeException) {
      throw localRuntimeException;
    }
    catch (Throwable localThrowable){
    }
    throw new UndeclaredThrowableException(localThrowable);
  }
...
}

当你调用代理类的say方时,他调用的是InvokeHandler的invoke方法,并把自个做首参传了进去。
就这样吧.

package com.spring.aop; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class PaoxyFactory { private Object proxy; public Object getProxy() { /** * ClassLoader loader * Class<?>[] interfaces, * InvocationHandler h */ ClassLoader classLoader = proxy.getClass().getClassLoader(); Class<?>[] interfaces = proxy.getClass().getInterfaces(); InvocationHandler h = new InvocationHandler() { /** * 代理对象 * @param proxy the proxy instance that the method was invoked on * * 代理对象需实现的方法 或者说目标对象需要重写的方法 * @param method the {@code Method} instance corresponding to * the interface method invoked on the proxy instance. The declaring * class of the {@code Method} object will be the interface that * the method was declared in, which may be a superinterface of the * proxy interface that the proxy class inherits the method through. * 对应方法中的参数 * @param args an array of objects containing the values of the * arguments passed in the method invocation on the proxy instance, * or {@code null} if interface method takes no arguments. * Arguments of primitive types are wrapped in instances of the * appropriate primitive wrapper class, such as * {@code java.lang.Integer} or {@code java.lang.Boolean}. * * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //方法调用前输出 System.out.println("动态代理方法前"); //调用目标方法 Object invoke = method.invoke(proxy, args); //方法调用前输出 System.out.println("动态代理方法后"); return invoke; } }; Object o = Proxy.newProxyInstance(classLoader, interfaces, h); return o; } public void setProxy(Object proxy) { } public PaoxyFactory(Object proxy) { this.proxy = proxy; } } 以上代码中运行的时候会一直输出动态代理方法前是什么原因应该怎么修改
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值