ホームページ  >  記事  >  Java  >  マイバティスインターセプター

マイバティスインターセプター

(*-*)浩
(*-*)浩転載
2019-09-03 16:41:131938ブラウズ

インターセプターの機能の 1 つは、特定のメソッドの呼び出しをインターセプトできることです。インターセプトされたメソッドの実行の前後にロジックを追加するか、インターセプトされたメソッドを破棄して独自のメソッドを実行するかを選択できます。ロジックです。

マイバティスインターセプター

たとえば、mybatis の Executor には、BatchExecutor、ReuseExecutor、SimpleExecutor、および CachingExecutor といういくつかの実装があります。要件が満たされると、インターセプターを構築して独自のクエリ メソッドを実装できます。インターセプターは通常、aop を使用して動的に実装されます。

インターセプターの原則

mybatis の場合、インターセプター インターフェイスを通じて独自のインターセプターを定義できます。インターセプタ インターフェイスの定義:

package org.apache.ibatis.plugin;
import java.util.Properties; 
public interface Interceptor { 
    Object intercept(Invocation invocation) throws Throwable; 
    Object plugin(Object target);
    void setProperties(Properties properties);
}

プラグイン メソッドは主にターゲット オブジェクトをカプセル化するために使用され、このメソッドを通じてインターセプトするかどうかを決定し、どのような種類のターゲット オブジェクトを返すかを決定できます。

インターセプトメソッドはインターセプト時に実行されるメソッドです。 setProperties は主に、設定ファイル内のプロパティを指定するために使用されます。このメソッドは、Configuration が現在のインターセプタを初期化するときに実行されます。mybatis には、静的メソッドの Wrap を含むプラグイン クラスがあります。このメソッドを通じて、オブジェクトがオブジェクトかどうかを判断できます。返される対象はオブジェクトまたはエージェントです。

package org.apache.ibatis.plugin;
 
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.ibatis.reflection.ExceptionUtil;
 
public class Plugin implements InvocationHandler {
 
    private Object target;
    private Interceptor interceptor;
    private Map<Class<?>, Set<Method>> signatureMap;
 
    private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {
        this.target = target;
        this.interceptor = interceptor;
        this.signatureMap = signatureMap;
    }
 
    public static Object wrap(Object target, Interceptor interceptor) {
        //解析获取需要拦截的类以及方法{*}
        Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
        Class<?> type = target.getClass();
        //解析type是否存在需要拦截的接口{*}
        Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
        //决定返回的对象是否为代理{*}
        if (interfaces.length > 0) {
            return Proxy.newProxyInstance(
                type.getClassLoader(),
                interfaces,
                new Plugin(target, interceptor, signatureMap));
        }
        //返回原目标对象
        return target;
    }
 
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        try {
            Set<Method> methods = signatureMap.get(method.getDeclaringClass());
            //如果当前执行的方法是定义的需要拦截的方法,则把目标对象,要拦截的方法以及参数封装为一个Invocation对象传递给拦截器方法intercept;
            //Invocation中定义了定义了一个proceed方法,其逻辑就是调用当前方法,所以如果在intercept中需要继续调用当前方法的话可以调用invocation的procced方法;
            if (methods != null && methods.contains(method)) {
                return interceptor.intercept(new Invocation(target, method, args));
            }
            return method.invoke(target, args);
        } catch (Exception e) {
            throw ExceptionUtil.unwrapThrowable(e);
        }
    }
 
    //根据注解解析需要拦截的方法
    //两个重要的注解:@Intercepts以及其值其值@Signature(一个数组)
    //@Intercepts用于表明当前的对象是一个Interceptor
    //@Signature则表明要拦截的接口、方法以及对应的参数类型。
    private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
        Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
        if (interceptsAnnotation == null) { // issue #251
            throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
        }
        Signature[] sigs = interceptsAnnotation.value();
        Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>();
        for (Signature sig : sigs) {
            Set<Method> methods = signatureMap.get(sig.type());
            if (methods == null) {
                methods = new HashSet<Method>();
                signatureMap.put(sig.type(), methods);
            }
            try {
                Method method = sig.type().getMethod(sig.method(), sig.args());
                methods.add(method);
            } catch (NoSuchMethodException e) {
                throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);
            }
        }
        return signatureMap;
    }
 
    private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>>  signatureMap) {
        Set<Class<?>> interfaces = new HashSet<Class<?>>();
        while (type != null) {
            for (Class<?> c : type.getInterfaces()) {
                if (signatureMap.containsKey(c)) {
                    interfaces.add(c);
                }
            }
            type = type.getSuperclass();
        }
        return interfaces.toArray(new Class<?>[interfaces.size()]);
    }
}

インターセプター インスタンス

package com.mybatis.interceptor;
 
import java.sql.Connection;
import java.util.Properties;
 
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
 
@Intercepts( {
@Signature(method = "query", type = Executor.class, args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class })}) 
public class TestInterceptor implements Interceptor {
    public Object intercept(Invocation invocation) throws Throwable {
        Object result = invocation.proceed();
        return result;
    }
 
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
 
    public void setProperties(Properties properties) {
        String p = properties.getProperty("property");
    }
}

まず、@Intercepts を使用してこれをインターセプターとしてマークし、@Signatrue を通じてインターセプト ポイントを設計します。インターセプター Executor インターフェイスのパラメーター タイプは次のとおりです。 MappedStatement、Object、RowBounds、および ResultHandler のクエリ メソッド。インターセプト メソッドは、呼び出しのプロシージャ メソッドを呼び出して、現在のメソッド呼び出しを正常に実行します。

インターセプタの登録

インターセプタの登録は、Mybatis 設定ファイルの plugins 要素の下にある plugin 要素を通じて実行されます。Mybatis は登録で定義されています。インターセプターを使用する場合、対応するインターセプターの下にあるすべてのプロパティは、最初にインターセプターの setProperties メソッドを通じて挿入されます。例:

<plugins>
    <plugin interceptor="com.mybatis.interceptor.TestInterceptor">
        <property name="property" value="拦截器配置"/>
    </plugin>
</plugins>

以上がマイバティスインターセプターの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はcsdn.netで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。