• java JDK动态代理


    博客已迁移到CSDN《https://blog.csdn.net/qq_33375499

    /**
    * jdk 动态代理:基于接口,动态代理类需要再运行时指定所代理对象实现的接口,客户端在调用动态代理对象的方法
    * 时,调用请求会将请求自动转发给 InvocationHandler对象的invoke()方法,由invoke()方法来实现
    * 对请求的统一处理
    * 1.创建一个接口subject
    * 2.创建一个需要被代理的对象,对象实现subject
    * 3.创建 invoactionHandler 对象,该对象有一个Object属性,用于接收需要被代理的真实对象,
    * 该接口作为代理实列的调用处理类
    * 4.创建需要被代理的对象,将该对象传入 invocationHandler中
    * 5.使用Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
    * 返回一个Class类型的代理类,在参数中需要提供类加载器并需要指定代理的接口数组(与真实代理对象实现的接口列表一致)
    */
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Proxy;
    
    public class JDKProxy {
        public static void main(String[] args) {
            JDKEntryInterface entry = new JDKEntry("LiXiaolong");
            InvocationHandler handler = new JDKInvocationHandler();
            ((JDKInvocationHandler) handler).setObject(entry);
            JDKEntryInterface proxy = (JDKEntryInterface) Proxy.newProxyInstance(
                    JDKEntryInterface.class.getClassLoader(),
                    new Class[]{ JDKEntryInterface.class },
                    handler
            );
            proxy.exercise();
        }
    }
    1.创建一个接口
    public interface JDKEntryInterface {
        void exercise();
    }
    2.创建一个需要被代理的对象,对象实现 JDKEntryInterface
    public class JDKEntry implements JDKEntryInterface {
        private String name;
    
        public JDKEntry(String name){
            this.name = name;
        }
    
        @Override
        public void exercise() {
            System.out.println("my name is " + name + ". I want to exercise");
        }
    }

    3.创建 invoactionHandler 对象,该对象有一个Object属性,用于接收需要被代理的真实对象,
    该接口作为代理实列的调用处理类
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    
    public class JDKInvocationHandler implements InvocationHandler {
        private Object object;
    
        public void setObject(Object object) {
            this.object = object;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("---------执行前动作-----------");
            Object result = method.invoke(this.object, args);
            System.out.println("---------执行后动作-----------");
            return result;
        }
    }
    4.创建需要被代理的对象,将该对象传入 invocationHandler中
    JDKEntryInterface entry = new JDKEntry("LiXiaolong");
            InvocationHandler handler = new JDKInvocationHandler();
            ((JDKInvocationHandler) handler).setObject(entry);
    5.使用Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)

    返回一个Class类型的代理类,在参数中需要提供类加载器并需要指定代理的接口数组(与真实代理对象实现的接口列表一致)
    JDKEntryInterface proxy = (JDKEntryInterface) Proxy.newProxyInstance(
                    JDKEntryInterface.class.getClassLoader(),
                    new Class[]{ JDKEntryInterface.class },
                    handler
            );

       6.执行方法

    proxy.exercise();

    执行结果:

    ---------执行前动作-----------
    my name is LiXiaolong. I want to exercise
    ---------执行后动作-----------
    
     
  • 相关阅读:
    [MS POST]Visual Studio Tips and Tricks
    Mono
    网络编程 socket编程
    Project Properties
    Review Error Handling
    [ILDASM Boxing]从进一步了解Struct和Class的不同学到的
    Steps to Bind VS solution to Source Control
    不已0开头的数字正则
    Jquery 解决移动端onclick事件300ms延迟问题
    毫秒转换日期
  • 原文地址:https://www.cnblogs.com/www-123456/p/11354792.html
Copyright © 2020-2023  润新知