使用InvocationHandler和Proxy而不使用接口

时间:2012-05-26 09:22:08

标签: java reflection dynamic proxy

  

可能重复:
  Dynamic proxy for concrete classes

一旦我创建了一个InvocationHandler实现,我可以将它与Proxy.newProxyInstance函数一起使用,以使对象的行为方式不同。在这种情况下,我必须创建一个接口及其实现类来使用Proxy.newProxyInstance函数:

MyInterface objDest = Proxy.newProxyInstance(MyInterfaceImpl.class.getClassLoader(),   
        MyInterfaceImpl.class.getInterfaces(),   
        new MyInvocationHandler(new MyInterfaceImpl()));  

是否可以使用没有接口的简单类直接执行相同的任务?

1 个答案:

答案 0 :(得分:1)

您应该使用CGLIB在具体类上创建动态代理。