首页  

Java动态代理InvocationHandler例子     所属分类 java 浏览量 1029
RPC框架 客户端 声明一个接口 ,就可以发起远程调用
动态代理 动态生成类

java.lang.reflect.Proxy
public static Object newProxyInstance(ClassLoader loader,Class< ?>[] interfaces,InvocationHandler h)
 
InvocationHandler
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable
                                        
private static interface Hello{
		String sayHello(String name);
}
实现 InvocationHandler invoke 方法 ,动态创建代理类




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

public class InvocationHandlerSimpleTest {

    public static void main(String[] args) throws Exception {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Class<?>[] interfaces = new Class[] {Hello.class};
        InvocationHandler h = new MyInvocationHandler();       
        Hello hello = (Hello)Proxy.newProxyInstance(loader, interfaces, h);		
        System.out.println(hello.sayHello("dyyx"));
        System.out.println(hello.sayHello("tiger"));
    }
	
    private static class MyInvocationHandler implements InvocationHandler{		
        public Object invoke(Object proxy, Method method, Object[] args)throws Throwable{		     
            if("sayHello".equals(method.getName())) {
                return "hello,"+args[0];
             }			 
             throw new Exception("not support method,"+method.getName());
        }
    }
    private static interface Hello{
        String sayHello(String name);
    }
}

https://gitee.com/dyyx/hellocode/blob/master/src/proxy/InvocationHandlerSimpleTest.java

这个例子里,没有被代理对象
invoke 方法里也做了简化 , 没有 对 hashCode toString 等方法的处理

更完整的例子
https://gitee.com/dyyx/hellocode/blob/master/src/proxy/InvocationHandlerTest.java



再来一个有被代理对象的 , 调用前后打印日志 Hello realObject = new HelloImpl(); ClassLoader loader = realObject.getClass().getClassLoader(); Class<?>[] interfaces = realObject.getClass().getInterfaces(); InvocationHandler h = new MyInvocationHandler(realObject); Hello hello = (Hello)Proxy.newProxyInstance(loader, interfaces, h); System.out.println(hello.sayHello("dyyx")); private static class MyInvocationHandler implements InvocationHandler{ // 被代理对象 实际的对象 private final Hello realObject; public MyInvocationHandler(Hello realObject) { this.realObject = realObject; } public Object invoke(Object proxy, Method method, Object[] args)throws Throwable{ if("sayHello".equals(method.getName())) { System.out.println("invoke start,name="+args[0]); Object result = method.invoke(realObject, args); System.out.println("invoke done,result="+result); return result; } throw new Exception("not support method,"+method.getName()); } } https://gitee.com/dyyx/hellocode/blob/master/src/proxy/InvocationHandlerLogTest.java
更完善的例子,rpc 客户端 动态生成代理类 https://gitee.com/dyyx/nettyrpc/blob/master/src/main/java/com/dyyx/nettyrpc/client/proxy/ObjectProxy.java
RPC原理简介 jdk代理与cglib代理的区别 AOP要点整理

上一篇     下一篇
dubbo SPI 机制简介

dubbo各个模块简介

基于netty的RESTFUL框架

Linux常用命令汇总

Java类加载过程

2020热词中英文