RPC的动态代理实现
(1) 定义协议
public interface Person { void walk(); void sayHello(); }(2) 实现协议
public class MyInvocationHandler implements InvocationHandler { /* * 执行动态代理对象的所有方法时,都会被替换成执行如下的invoke方法 * proxy: 代表动态代理对象 * method: 代表正在执行的方法 * args: 代表实参 * * */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("---正在执行的方法:"+method); method.invoke(proxy,args); if (args != null){ System.out.println("--传入的实参为:"); for (Object arg:args){ System.out.println(arg); } }else{ } return null; } }(3) 测试
public class ProxyText { public static void main(String[] args) throws Exception{ InvocationHandler handler = new MyInvocationHandler(); Person p =(Person) Proxy.newProxyInstance(Person.class.getClassLoader(), new Class[]{Person.class},handler); // 动态代理对象的walk和sayhello p.walk(); p.sayHello(); } }