jdk proxy


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

public class AutoProxy {


    public static void main(String[] args) {


//        创建接口实现类的代理对象
        Class[] interfaces = {Person.class};
//        动态代理
        Person user = (Person) Proxy.newProxyInstance(AutoProxy.class.getClassLoader(), interfaces, new Invoke(new User()));
        user.eat();

        user.run();


    }
}

/**
 * 代理方法
 */
class Invoke implements InvocationHandler {

    private Object object;

    public Invoke(Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("methods before exe...." + method.getName());

        Object back = method.invoke(object, args);

        System.out.println("back = " + back);
        System.out.println("methods after exe....");
        return back;
    }
}