【发布时间】:2011-07-07 05:37:31
【问题描述】:
我有一个关于 java 中的动态代理的问题。
假设我有一个名为Foo 的接口,其方法为execute,类为FooImpl implements Foo。
当我为Foo 创建代理时,我有类似的东西:
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class[] { Foo.class },
handler);
假设我的调用处理程序如下所示:
public class FooHandler implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) {
...
}
}
如果我的调用代码看起来像
Foo proxyFoo = (Foo) Proxy.newInstance(Foo.getClass().getClassLoader(),
new Class[] { Foo.class },
new FooHandler());
proxyFoo.execute();
如果代理可以从Foo接口拦截上述调用execute,那么FooImpl在哪里发挥作用?也许我以错误的方式看待动态代理。我想要的是能够从Foo 的具体实现中捕获execute 调用,例如FooImpl。这个可以吗?
非常感谢
【问题讨论】: