天天看点

反射_应用:Java动态代理

动态代理特别重要,Java的框架都爱这玩意儿!!!

动态代理特别重要,Java的框架都爱这玩意儿!!!

动态代理特别重要,Java的框架都爱这玩意儿!!!

Java动态代理

  • 动态代理是指客户通过代理类来调用其它对象的方法,并且是在程序运行时根据需要动态创建目标类的代理对象。
  • 动态代理使用场合:

    1)调式

    2)远程方法调用

  • 代理设计模式的原理:

    使用一个代理将对象包装起来,然后用该代理对象取代原始对象。任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。

动态代理之前先说一个静态代理的例子:

/**静态代理模式*/
  //接口
  interface ClothFactory{
    void productCloth();
  }
  //被代理类
  class NikeClothFactory implements ClothFactory{
      @Override
      public void productCloth() {
          System.out.println("Nike工厂生产衣服");
      }
  }
  //代理类
  class ProxyFactory implements ClothFactory{
      ClothFactory cf;
      //代理类构造器
      //创建代理类的对象,实际传入的是被代理类的对象
      public ProxyFactory(ClothFactory cf) {
          this.cf = cf;
      }

      @Override
      public void productCloth() {
          System.out.println("代理类开始执行收代理费 1000");
          //执行的是被代理对象的方法
          cf.productCloth();
      }
  }

public class TestClothProduct {
    public static void main(String[] args) {
        NikeClothFactory ncf = new NikeClothFactory();
        ProxyFactory pf = new ProxyFactory(ncf);
        pf.productCloth();
    }
}
      

动态代理

/**动态代理的使用,体会反射是动态语言的关键*/
interface Subject {
    void action();
}

//被代理类
class RealSubject implements Subject {

    @Override
    public void action() {
        System.out.println("我是被代理类,记得要执行我哦");
    }
}

//动态代理类
class MyInvocationHandler implements InvocationHandler {
    //实现了接口的被代理类的对象的声明
    Object obj;

    //1、给被代理的对象实例化;2、返回一个代理类的对象
    public Object blind(Object obj) {
        this.obj = obj;
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
    }
    //当通过代理类的对象发起对被重写的方法的调用时,都会转化为对如下的invoke方法的调用
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object returnVal = method.invoke(obj,args);
        return returnVal;
    }
}

public class TestProxy {
    public static void main(String[] args) {
        //1|被代理对象
        RealSubject rs = new RealSubject();
        //2、创建一个实现了InvocationHandler接口的类的对象
        MyInvocationHandler handler = new MyInvocationHandler();
        //3、调用blind( )方法,动态的返回一个同样实现了rs所在类实现的接口Subject的代理类的对象。
        Object obj = handler.blind(rs);
        Subject su = (Subject) obj;//此时的su就是代理类的对象
        su.action();//会转到对InvocationHandler接口是实现类的invoke( )方法的调用

        //再举一例
       NikeClothFactory nikeClothFactory = new NikeClothFactory();
        ClothFactory clothProxy = (ClothFactory) handler.blind(nikeClothFactory);
        clothProxy.productCloth();
    }
}