mybatis中的擴(kuò)展實(shí)現(xiàn)源碼解析
前言
最近項(xiàng)目中需要用到mybatis的擴(kuò)展,就深入看了下mybatis的實(shí)現(xiàn),對(duì)其靈活性和擴(kuò)展性的設(shè)計(jì)思想還是非常佩服的
首先說一下mybatis的攔截器使用方法:繼承其Intercepter接口,實(shí)現(xiàn)org.apache.ibatis.plugin.Interceptor#intercept方法,在其中或者對(duì)其要執(zhí)行的方法進(jìn)行攔截,或者對(duì)返回值進(jìn)行解析
同時(shí)基于org.apache.ibatis.plugin.Intercepts和org.apache.ibatis.plugin.Signature這兩個(gè)注解來決定,對(duì)哪些執(zhí)行器的哪些方法進(jìn)行攔截
先看下攔截器的核心接口
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;
Object plugin(Object target);
void setProperties(Properties properties);
}
其中intercept方法是核心方法,攔截器的實(shí)現(xiàn),plugin方法是用于配置哪些對(duì)哪些執(zhí)行器進(jìn)行攔截
繼續(xù)看源碼,可以看到mybatis的攔截是使用了jdk的動(dòng)態(tài)代理實(shí)現(xiàn)的,本質(zhì)上是一種代理機(jī)制
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的這個(gè)Plugin就是代理類,這個(gè)代理類是在org.apache.ibatis.plugin.Interceptor#plugin方法中初始化的(調(diào)用org.apache.ibatis.plugin.Plugin#wrap),一個(gè)Plugin包含一個(gè)Intercepter,以及該Intercepter相關(guān)的注解配置信息,當(dāng)對(duì)攔截對(duì)象的對(duì)應(yīng)方法進(jìn)行執(zhí)行的時(shí)候,都會(huì)根據(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)用的,我們可以看到,如果一個(gè)應(yīng)用中注冊(cè)了多個(gè)攔截器,那么實(shí)際上是會(huì)進(jìn)行一個(gè)for循環(huán)的加載,由于上面說到了,加載一次,本質(zhì)上是對(duì)mybatis的執(zhí)行期進(jìn)行一次代理包裝,那么加載多次的話,就會(huì)代理包裝多次,實(shí)際上就是一種多重代理了,這樣就保證了每次調(diào)用都會(huì)按照代理順序進(jìn)行調(diào)用和返回的處理
可以看到,在做這些mybatis執(zhí)行器初始化的時(shí)候,都會(huì)進(jìn)行攔截器鏈的加載

至此,mybatis基于jdk動(dòng)態(tài)代理的擴(kuò)展實(shí)現(xiàn)方法就了解清楚了,其靈活性在于,它抽象了執(zhí)行器的概念,并且攔截器的攔截方法也是固定的,我們可以對(duì)不同執(zhí)行器的不同方法進(jìn)行攔截,而對(duì)這些擴(kuò)展點(diǎn)進(jìn)行擴(kuò)展卻不用寫多個(gè)方法實(shí)現(xiàn)多個(gè)方法,只需要實(shí)現(xiàn)一個(gè)接口就可以搞定了!
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
java程序員必會(huì)的遠(yuǎn)程debug教程
這篇文章主要為大家介紹了java程序員必會(huì)的遠(yuǎn)程debug教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
基于spring-security 401 403錯(cuò)誤自定義處理方案
這篇文章主要介紹了基于spring-security 401 403錯(cuò)誤自定義處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Spring Boot RabbitMQ 延遲消息實(shí)現(xiàn)完整版示例
本篇文章主要介紹了Spring Boot RabbitMQ 延遲消息實(shí)現(xiàn)完整版示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
詳解Java MyBatis 插入數(shù)據(jù)庫返回主鍵
這篇文章主要介紹了詳解Java MyBatis 插入數(shù)據(jù)庫返回主鍵,有興趣的可以了解一下。2017-01-01
rabbitmq學(xué)習(xí)系列教程之消息應(yīng)答(autoAck)、隊(duì)列持久化(durable)及消息持久化
這篇文章主要介紹了rabbitmq學(xué)習(xí)系列教程之消息應(yīng)答(autoAck)、隊(duì)列持久化(durable)及消息持久化,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
Java多線程Thread , Future , Callable ,
本文主要介紹了Java多線程Thread , Future , Callable , FutureTask的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
java中Iterator和ListIterator實(shí)例詳解
這篇文章主要介紹了java中Iterator和ListIterator實(shí)例詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12

