本文介绍了Java动态代理 - 如何参考具体类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设我有一个名为 Foo 的界面,一个方法执行和class FooImpl实现Foo



当我为 Foo 创建一个代理,我有一些类似的东西:

  Foo f =(Foo)Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class [] {Foo.class},
handler);

假设我的调用处理程序如下所示:

  public class FooHandler实现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 界面, FooImpl 进入哪里?也许我以错误的方式看动态代理。我想要的是从 Foo 的具体实现中获取执行调用,例如 FooImpl 。可以这样做吗?



非常感谢

解决方案

使用动态代理的截取方法是:

  public class FooHandler implements InvocationHandler {
private Object realObject;

public FooHandler(Object real){
realObject = real;
}


public Object invoke(Object target,Method method,Object [] arguments)throws Throwable {
if(execute.equals(method.getName ()){
//截取方法名为execute
}

//调用原始方法
return method.invoke(realObject,arguments);
}
}

通过以下方式调用代理:

  Foo foo =(Foo)Proxy.newProxyInstance(
Foo.class.getClassLoader(),
new Class [] {Foo。 class},
new FooHandler(new FooImpl()));


I have a question relating to dynamic proxies in java.

Suppose I have an interface called Foo with a method execute and class FooImpl implements Foo.

When I create a proxy for Foo and I have something like:

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
                                     new Class[] { Foo.class },
                                     handler);

Suppose my invocation handler looks like:

public class FooHandler implements InvocationHandler {
    public Object invoke(Object proxy, Method method, Object[] args) {
        ...
    }
}

If my invocation code looks something like

Foo proxyFoo = (Foo) Proxy.newInstance(Foo.getClass().getClassLoader(),
                                       new Class[] { Foo.class },
                                       new FooHandler());
proxyFoo.execute();

If the proxy can intercept the aforementioned call execute from the Foo interface, where does the FooImpl come in to play? Maybe I am looking at dynamic proxies in the wrong way. What I want is to be able to catch the execute call from a concrete implementation of Foo, such as FooImpl. Can this be done?

Many thanks

解决方案

The way to intercept methods using dynamic proxies are by:

public class FooHandler implements InvocationHandler {
    private Object realObject;

    public FooHandler (Object real) {
        realObject=real;
    }


    public Object invoke(Object target, Method method, Object[] arguments) throws Throwable {
        if ("execute".equals(method.getName()) {
            // intercept method named "execute"
        }

        // invoke the original methods
        return method.invoke(realObject, arguments);
    }
}

Invoke the proxy by:

Foo foo = (Foo) Proxy.newProxyInstance(
            Foo.class.getClassLoader(),
            new Class[] {Foo.class},
            new FooHandler(new FooImpl()));

这篇关于Java动态代理 - 如何参考具体类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:36