zoukankan      html  css  js  c++  java
  • java-中的代理

    静态代理:

     例子:

    接口:

    public interface InterfaceBase {
        void proxy();
    }

    接口实现类:

    public class InterfaceBaseReal implements InterfaceBase{
        public void proxy() {
            System.out.println("InterfaceBase......");
        }
    }

    代理类:

    public class InterfaceBaseRealProxy  implements InterfaceBase{
        //调用之前的功能
        private InterfaceBaseReal interfaceBaseReal;
        //通过构造函数进行被代理类的初始化
        public InterfaceBaseRealProxy(InterfaceBaseReal interfaceBaseReal) {
            this.interfaceBaseReal= interfaceBaseReal;
        }
    
        public void proxy() {
            System.out.println("InterfaceBaseRealProxy....before");
            //调用被代理类的功能,保留原来的不变
            interfaceBaseReal.proxy();
            System.out.println("InterfaceBaseRealProxy....after");
        }
    }

    动态代理:

    动态代理的实现步骤:
        必须有的类:被代理的类
                    被代理的类所实现的接口
                    实现InvocationHandler接口的类
        流程:实现IncationHandler接口,
                接口要求:通过构造函数传入被代理的类
                          在重写invoke函数里面进行被代理类方法的调用
                          编写增强逻辑
                通过Proxy类的静态方法获得代理类:
                    Foo f =(Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),new Class<?>[] { Foo.class },handler); 
                                                         获取类加载器,哪个类的都可以, 一般是使用当前类
                                                         被代理类所对应接口的Class对象数组,可以多个
                                                         实现InvocationHandler的类实例

    第二个接口实现类:

    public class InterfaceBaseReal2 implements InterfaceBase{
        public void proxy() {
            System.out.println("InterfaceBase22222......");
        }
    }

    处理调用程序:

    //实现InvacationHandler接口的类,这个类被成为调用处理程序
    public class BaseInvocationHandler implements InvocationHandler {
        //invocationHandler实现类里面必须传入被代理的类(实现被代理接口的类)
        //这里使用对象的顶层基类,主要可以传入不同的对象
        private Object  object;
        //通过构造函数传入
        public BaseInvocationHandler(Object  object) {
            this.object= object;
        }
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("InterfaceBaseRealProxy....before");
            //调用被代理类的功能,保留原来的不变
            Object re=method.invoke(object,args);
            System.out.println("InterfaceBaseRealProxy....after");
           //返回真是代理对象,也可以通过 return proxy
            return re;
        }
    }

    获取代理实例:

    public class HandlerTest {
        public static void main(String[] args) {
            //被代理类所实现的接口的Class对象
            Class[]inter={InterfaceBase.class};
            //被代理的类的实例
            InterfaceBase interfaceBase=new InterfaceBaseReal();
            //实现接口InvocationHandler的类的实例(必须传入被代理的类)
            BaseInvocationHandler bh=new BaseInvocationHandler(interfaceBase);
            //通过Proxy的静态函数得到代理实例
            InterfaceBase ibproxy=(InterfaceBase) Proxy.newProxyInstance(InterfaceBase.class.getClassLoader(),inter,bh);
            ibproxy.proxy();
    
            System.out.println(ibproxy.getClass());
            System.out.println("2222222222222222222222222222222222");
            //第二个被代理的类
            InterfaceBase interfaceBase2=new InterfaceBaseReal2();
            BaseInvocationHandler bh2 =new BaseInvocationHandler(interfaceBase2);
            InterfaceBase ibproxy2 =(InterfaceBase)Proxy.newProxyInstance(HandlerTest.class.getClassLoader(),inter,bh2);
            ibproxy2.proxy();
        }
    }

    注意:因为我们的处理调用程序当中设置传入的对象为Object,所以可以传入不同的对象

  • 相关阅读:
    leetcode108 Convert Sorted Array to Binary Search Tree
    leetcode98 Validate Binary Search Tree
    leetcode103 Binary Tree Zigzag Level Order Traversal
    leetcode116 Populating Next Right Pointers in Each Node
    Python全栈之路Day15
    Python全栈之路Day11
    集群监控
    Python全栈之路Day10
    自动部署反向代理、web、nfs
    5.Scss的插值
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13309017.html
Copyright © 2011-2022 走看看