mybatis中的擴展實現(xiàn)源碼解析
前言
最近項目中需要用到mybatis的擴展,就深入看了下mybatis的實現(xiàn),對其靈活性和擴展性的設計思想還是非常佩服的
首先說一下mybatis的攔截器使用方法:繼承其Intercepter接口,實現(xiàn)org.apache.ibatis.plugin.Interceptor#intercept
方法,在其中或者對其要執(zhí)行的方法進行攔截,或者對返回值進行解析
同時基于org.apache.ibatis.plugin.Intercepts
和org.apache.ibatis.plugin.Signature
這兩個注解來決定,對哪些執(zhí)行器的哪些方法進行攔截
先看下攔截器的核心接口
public interface Interceptor { Object intercept(Invocation invocation) throws Throwable; Object plugin(Object target); void setProperties(Properties properties); }
其中intercept方法是核心方法,攔截器的實現(xiàn),plugin方法是用于配置哪些對哪些執(zhí)行器進行攔截
繼續(xù)看源碼,可以看到mybatis的攔截是使用了jdk的動態(tài)代理實現(xiàn)的,本質(zhì)上是一種代理機制
public class Plugin implements InvocationHandler { private final Object target; private final Interceptor interceptor; private final 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(); Class<?>[] interfaces = getAllInterfaces(type, signatureMap); if (interfaces.length > 0) { return Proxy.newProxyInstance( type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap)); } return target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { 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); } catch (Exception e) { throw ExceptionUtil.unwrapThrowable(e); } } ...... }
mybatis的這個Plugin就是代理類,這個代理類是在org.apache.ibatis.plugin.Interceptor#plugin
方法中初始化的(調(diào)用org.apache.ibatis.plugin.Plugin#wrap
),一個Plugin包含一個Intercepter,以及該Intercepter相關(guān)的注解配置信息,當對攔截對象的對應方法進行執(zhí)行的時候,都會根據(jù)這些注解配置來判斷是否需要執(zhí)行該代理攔截(org.apache.ibatis.plugin.Plugin#invoke
)
再看下plugin是如何被加載的:
public class InterceptorChain { private final List<Interceptor> interceptors = new ArrayList<Interceptor>(); public Object pluginAll(Object target) { for (Interceptor interceptor : interceptors) { target = interceptor.plugin(target); } return target; } public void addInterceptor(Interceptor interceptor) { interceptors.add(interceptor); } public List<Interceptor> getInterceptors() { return Collections.unmodifiableList(interceptors); } }
org.apache.ibatis.plugin.Interceptor#plugin
是在org.apache.ibatis.plugin.InterceptorChain#pluginAll
方法中調(diào)用的,我們可以看到,如果一個應用中注冊了多個攔截器,那么實際上是會進行一個for循環(huán)的加載,由于上面說到了,加載一次,本質(zhì)上是對mybatis的執(zhí)行期進行一次代理包裝,那么加載多次的話,就會代理包裝多次,實際上就是一種多重代理了,這樣就保證了每次調(diào)用都會按照代理順序進行調(diào)用和返回的處理
可以看到,在做這些mybatis執(zhí)行器初始化的時候,都會進行攔截器鏈的加載
至此,mybatis基于jdk動態(tài)代理的擴展實現(xiàn)方法就了解清楚了,其靈活性在于,它抽象了執(zhí)行器的概念,并且攔截器的攔截方法也是固定的,我們可以對不同執(zhí)行器的不同方法進行攔截,而對這些擴展點進行擴展卻不用寫多個方法實現(xiàn)多個方法,只需要實現(xiàn)一個接口就可以搞定了!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Java BufferedImage轉(zhuǎn)換為MultipartFile方式
這篇文章主要介紹了Java BufferedImage轉(zhuǎn)換為MultipartFile方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot使用Thymeleaf自定義標簽的實例代碼
這篇文章主要介紹了SpringBoot使用Thymeleaf自定義標簽的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java函數(shù)式編程(三):列表的轉(zhuǎn)化
這篇文章主要介紹了Java函數(shù)式編程(二):列表的轉(zhuǎn)化,lambda表達式不僅能幫助我們遍歷集合,并且可以進行集合的轉(zhuǎn)化,需要的朋友可以參考下2014-09-09Windows下java、javaw、javaws以及jvm.dll等進程的區(qū)別
這篇文章主要介紹了Windows下java、javaw、javaws以及jvm.dll等進程的區(qū)別,本文分別講解了它們的作用并給出代碼實例,最后做出了區(qū)別總結(jié),需要的朋友可以參考下2015-03-03MybatisPlus中@TableField注解的使用詳解
這篇文章主要介紹了MybatisPlus中@TableField注解的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09