• 对象代理模板Java代码


    package cn.zlc.domain;

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

    public class DyProxy implements InvocationHandler {

     // 原对象(真实对象,目标对象)StudentServiceImpl--->StudentStaticProxy(代理对象)
     private Object targetObject;

     // 把真实对象(原对象)通过此方法传入
     // 此方法的返回值为代理对象
     public Object getProxy(Object targetObject) {

      this.targetObject = targetObject;
      // 根据原对象产生代理对象
      // 参数1:原对象所属类的加载器
      // 参数2:原对象所属类的借口
      // 参数3:实现了InvocationHandler接口的对象
      return Proxy.newProxyInstance(this.targetObject.getClass()
        .getClassLoader(),
        this.targetObject.getClass().getInterfaces(), this);

     }

     @Override
     public Object invoke(Object proxy, Method method, Object[] args)
       throws Throwable {

      // 声明一个返回值变量
      Object ret = null;

      try {

       
       // 回调原对象中的方法
       
       /* if(method.getName().equals("addStudent")){
       check();
        } */
       
       
       ret = method.invoke(this.targetObject, args);
       
      

       throw new RuntimeException();

      } catch (Exception e) {
               
       check();
       e.printStackTrace();
      }

      return ret;
     }

     public void check() {

      System.out.println("============安全性检查============");
     }

    }

  • 相关阅读:
    Django model 常用方法记录
    程序员的注意事项
    硬件天使的使用
    你是否应该成为一名全栈工程师?
    web技术
    6个处理上面代码异味的重构方法(手法)
    git 命定
    ie console报错
    apache 省略index.php访问
    myisam和innodb的区别
  • 原文地址:https://www.cnblogs.com/alvin-perfect/p/4390065.html
Copyright © 2020-2023  润新知