2021年9月26日 | Leave a comment 问题: 解决: 完整代码: https://gitee.com/giant35/proxy1 App.java package demo; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; public class App { public static void main(String[] args) { final InvocationHandler h = new TestInvocationHandler(); C c = (C) Proxy.newProxyInstance(App.class.getClassLoader(), new Class[]{C.class}, h); var r = c.plus(1, 2); System.out.println("r:" + r); } } 12345678910111213 package demo; import java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy; public class App { public static void main(String[] args) { final InvocationHandler h = new TestInvocationHandler(); C c = (C) Proxy.newProxyInstance(App.class.getClassLoader(), new Class[]{C.class}, h); var r = c.plus(1, 2); System.out.println("r:" + r); }} C.java package demo; public interface C { int plus(int a, int b); } 12345 package demo; public interface C { int plus(int a, int b);} TestInvocationHandler.java package demo; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class TestInvocationHandler implements InvocationHandler { @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { System.out.println("proxy.class:" + proxy.getClass() + " method: " + method + " args:" + StringUtils.join(args, ",")); return (int) args[0] + (int) args[1]; } } 1234567891011121314 package demo; import org.apache.commons.lang3.StringUtils; import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method; public class TestInvocationHandler implements InvocationHandler { @Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { System.out.println("proxy.class:" + proxy.getClass() + " method: " + method + " args:" + StringUtils.join(args, ",")); return (int) args[0] + (int) args[1]; }} 参考: