站長資訊網
最全最豐富的資訊網站

mybatis攔截器

攔截器的一個作用就是我們可以攔截某些方法的調用,我們可以選擇在這些被攔截的方法執行前后加上某些邏輯,或者丟棄這些被攔截的方法而執行自己的邏輯。

mybatis攔截器

如對于mybatis的Executor,有幾種實現:BatchExecutor,ReuseExecutor、SimpleExecutor和CachingExecutor,當這幾種Executor接口的query方法無法滿足我們的要求的時候,我們就可以建立一個攔截器來實現自己的query方法;攔截器一般采用aop動態實現。

攔截器原理

對于mybatis,我們可以通過interceptor接口定義自己的攔截器。interceptor接口定義:

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); }

plugin方法主要是用于封裝目標對象,通過該方法我們可以決定是否要進行攔截進而決定返回什么樣的目標對象。

intercept方法就是要進行攔截的時候執行的方法。setProperties主要用于在配置文件中指定屬性,這個方法在Configuration初始化當前的Interceptor時就會執行.在mybatis中有一個plugin類,該類包括靜態方法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標記了這是一個Interceptor,通過@Signatrue設計攔截點:攔截Executor接口中參數類型為MappedStatement、Object、RowBounds和ResultHandler的query方法;intercept方法調用invocation的proceed方法,使當前方法正常調用。

攔截器的注冊

注冊攔截器是通過在Mybatis配置文件中plugins元素下的plugin元素來進行的,Mybatis在注冊定義的攔截器時會先把對應攔截器下面的所有property通過Interceptor的setProperties方法注入。如:

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

贊(0)
分享到: 更多 (0)
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
日韩在线观看视频免费| 国语自产精品视频在线看| 久久精品男人影院| 精品人妻系列无码人妻免费视频| 久久精品日韩一区国产二区 | 国产亚洲精品美女久久久久久下载 | 亚洲精品乱码久久久久久蜜桃图片| **毛片免费观看久久精品| 亚洲精品在线观看视频| 久久国产精品二区99| 国产精品igao视频网| 久久亚洲国产精品一区二区| 中文字幕久精品免费视频| 国产呦小j女精品视频| 久久久亚洲精品蜜桃臀| 成人午夜视频精品一区| 精品久久综合1区2区3区激情| 精品国产日韩亚洲一区| 国产乱码伦精品一区二区三区麻豆| 国产精品主播一区二区| 国产伦精品一区二区免费| 精品久久久久久无码国产| 久久人搡人人玩人妻精品首页| 久久精品无码一区二区三区日韩 | 国产l精品国产亚洲区在线观看| 奇米精品一区二区三区在线观看| 国产精品99精品久久免费| 亚洲av无码乱码国产精品fc2| 久久九九久精品国产日韩经典| 久久亚洲AV午夜福利精品一区| 99精品在线视频| 亚洲一区二区三区精品视频| 国产精品中文字幕在线观看| 国产精品影音先锋| 人妻少妇精品无码专区| 精品综合久久久久久蜜月| 国产精品JIZZ在线观看无码| 国产午夜亚洲精品不卡免下载| 制服丝袜日韩中文字幕在线| 国产免费久久精品99re丫y| 久久精品中文字幕|