定义一个接口
public interface TestService {
String add(String param1);
String query(String param1, String param2);
}
实现 InvocationHandler接口
@Data public class TestProxyimplements InvocationHandler { private Class interfaceClass; @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String reslut = "invoke " + interfaceClass.getName() + "." + method.getName() + ".args:" + args + " , do anything .."; System.out.println(reslut); return reslut; } public Object getProxy(){ final Class[] interfaces = {interfaceClass}; return Proxy.newProxyInstance(this.getClass().getClassLoader(), interfaces, this); } public static void main(String[] args) { TestProxy testProxy = new TestProxy(); testProxy.setInterfaceClass(TestService.class); TestService testService = (TestService)testProxy.getProxy(); testService.add("000000000000000"); testService.query("000000000000000","11111111111111"); } }
输出
invoke com.test.one.TestService.add.args:[Ljava.lang.Object;@7960847b , do anything ..
invoke com.test.one.TestService.query.args:[Ljava.lang.Object;@6a6824be , do anything ..



