简体   繁体   English

Java动态代理问题

[英]java dynamic proxy issue

I'm learning about java dynamic proxy, and here's my code: 我正在学习Java动态代理,这是我的代码:

//interface  Move.java
 public interface Move {
    public void testMove();
}

then the implementation class 然后是实现类

public class Tank implements Move{
    public void testMove() {
        System.out.println("test in Tank");
    }
}

followed by 其次是

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


public class MoveHandler implements InvocationHandler{
    private Object target;

    public MoveHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
        System.out.println("in MoveHandler");
        Object ret;
        try {
            ret = method.invoke(target, args);
        }catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        System.out.println("after MoveHandler");
        return ret;
    }
}

test class 测试班

public class Main {
    public static void main(String[] args) {
        Move test = new Tank();
        MoveHandler proxy = new MoveHandler(test);
        Move real = (Move) Proxy.newProxyInstance(test.getClass().getClassLoader(), test.getClass().getInterfaces(), proxy);
        real.testMove();
    }
}

I can get the right result when I run the Main class. 运行Main类时,我可以得到正确的结果。 If I change method.invoke(target, args) into method.invoke(proxy, args) , there will be thousand lines of exception and errors. 如果我将method.invoke(target, args)更改为method.invoke(proxy, args) ,将有数千行异常和错误。 What's the usage of the argument proxy in invoke(Object proxy, Method method, Object[] args) and how can I use it correctly? 参数代理invoke(Object proxy, Method method, Object[] args)的用法是什么,如何正确使用它?

The proxy argument is the object returned by Proxy.newProxyInstance() , on which the actual method ( testMove() ) is called. proxy参数是Proxy.newProxyInstance()返回的对象,在其上调用实际方法( testMove() )。 You usually don't need it, but it can be necessary to know which interfaces the proxy implements, for example. 您通常不需要它,但是例如,可能需要知道代理实现的接口。

Invoking the method on this argument is a really bad idea since it basically does a recursive method call : you invoke a method on a proxy, which calls the invocation handler, which calls the method on the proxy, which calls the handler, etc. 在此参数上调用方法是一个非常糟糕的主意,因为它基本上会进行递归方法调用:您在代理上调用一个方法,该方法调用调用处理程序,该调用处理程序在代理上调用该方法,然后调用处理程序,依此类推。

Using Java Reflection you create dynamic implementations of interfaces at runtime. 使用Java Reflection,您可以在运行时创建接口的动态实现。 You do so using the class java.lang.reflect.Proxy . 您可以使用类java.lang.reflect.Proxy The name of this class is why I refer to these dynamic interface implementations as dynamic proxies. 此类的名称是为什么我将这些动态接口实现称为动态代理的原因。 Dynamic proxies can be used for many different purposes, eg database connection and transaction management, dynamic mock objects for unit testing, and other AOP-like method intercepting purposes. 动态代理可用于许多不同目的,例如数据库连接和事务管理,用于单元测试的动态模拟对象以及其他类似于AOP的方法拦截目的。

Further explanation: 进一步说明:

The proxy parameter passed to the invoke() method is the dynamic proxy object implementing the interface. 传递给invoke()方法的proxy参数是实现接口的动态代理对象。 Most often you don't need this object. 大多数情况下,您不需要此对象。

This is from this tutorial , which explains in a lot more detail. 这是来自本教程的该教程进行了更详细的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM