• mybatis插件


    1. Interceptor 就是个接口,供其它插件实现这个接口有三个方法

    Object intercept(Invocation invocation)throws Throwable;

    传入一个Invocation 对象,我猜应该是指定拦截具体某个target的某个方法

    这里面应该写具体的拦截策略,befor after。调用invocation.proceed()执行具体的方法

    Object plugin(Object target);

    将目标对象与插件绑定

    void setProperties(Properties properties);

    设置属性

    2. Invocation 是一个对象类

    对象内部包括目标对象、待执行的方法、参数列表

    public Object proceed() 包装了一下method.invoke(target,args)

    3. Plugin这个类比较重要

    这个类有两个比较重要的方法,一个

    public staticObject wrap(Object target, Interceptor interceptor)

    这是个静态方法,作为工具用

    将target和interceptor两个对象包装在一起。返回代理类对象

    实现了Interceptor接口的对象中plugin方法可以调用这个静态方法来实现将intercepter和target绑定到一起

    这个方法调用了getSignatureMap方法来取写了antonation的方法,可能是通过注解来标注哪些类需要拦截吧

    另一个

    publicObject invoke(Object proxy, Method method, Object[] args)

    这个方法实现了InvocationHandler接口必须要实现这个方法,并且这个方法在调用对象的方法时是一定要执行的。

    这个方法里也比较简单,就是判断有没有拦截器,有执行拦截器,否则直接执行方法

    Set<Method> methods = signatureMap.get(method.getDeclaringClass());

         if(methods != null&& methods.contains(method)) {

            return interceptor.intercept(new Invocation(target, method, args));

         }

          return method.invoke(target, args);

    4. 有个问题就是这几个插件类具体怎么用

             1) 首先一定是要实现Interceptor,这个接口里应该有个成员变量来存target吧?通过plugin方法绑定。Plugin方法里应该是调用了Plugin. Wrap这个静态方法。将绑定后的类返回

             2) 用户使用时直接new 一个Plugin对象,将target、interceptor、signatureMap传进得去。这样再调用 
  • 相关阅读:
    如何保证你的路由器安全?
    Jmeter、fiddler、postman 如何模拟ajax请求
    U盘快速格式化和普通格式化有什么区别
    Layui button disabled
    TP连接数据库字符串方式
    Warning: require(D:wamp64wwwxxxpublic/../thinkphp/start.php): failed to open stream: No such file or directory in D:wamp64www xxx publicindex.php on line 17
    [AWS] EC2
    [AWS] EC2
    [AWS] EC2
    [AWS DA
  • 原文地址:https://www.cnblogs.com/liguangming/p/7053380.html
Copyright © 2020-2023  润新知