Java动态代理-如何引用具体类

发布于 2024-10-19 15:56:46 字数 1048 浏览 1 评论 0原文

我有一个关于java中动态代理的问题。

假设我有一个名为 Foo 的接口,其中包含一个 execute 方法,并且类 FooImpl 实现了 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();

如果代理可以拦截上述调用 executeFoo 接口来看,FooImpl 在哪里发挥作用?也许我以错误的方式看待动态代理。我想要的是能够从 Foo 的具体实现(例如 FooImpl)捕获 execute 调用。这可以做到吗?

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写下不归期 2024-10-26 15:56:46

使用动态代理拦截方法的方法是: 通过以下方式

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);
    }
}

调用代理:

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

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()));
别再吹冷风 2024-10-26 15:56:46

如果您想委托给某些 Foo 实现(例如 FooImpl),只需将您的 InitationHandler 设为其他 Foo 实例的包装器(传递给构造函数),然后发送 FooImpl 实例作为委托即可。然后,在 handler.invoke() 方法内,调用 method.invoke(delegate, args)。

If you want to delegate to some Foo implementation like FooImpl, just make your InvocationHandler a wrapper of an other Foo instance (passed to the constructor) and then send a FooImpl instance as the delegate. Then, inside the handler.invoke() method, call method.invoke(delegate, args).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文