简体   繁体   中英

java dynamic proxy issue

I'm learning about java dynamic proxy, and here's my code:

//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. If I change method.invoke(target, args) into method.invoke(proxy, args) , there will be thousand lines of exception and errors. What's the usage of the argument proxy in invoke(Object proxy, Method method, Object[] args) and how can I use it correctly?

The proxy argument is the object returned by Proxy.newProxyInstance() , on which the actual method ( testMove() ) is called. 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. You do so using the class 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.

Further explanation:

The proxy parameter passed to the invoke() method is the dynamic proxy object implementing the interface. Most often you don't need this object.

This is from this tutorial , which explains in a lot more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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