您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)怎么在mybatis中實(shí)現(xiàn)擴(kuò)展,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
先看下攔截器的核心接口
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)行攔截器鏈的加載
關(guān)于怎么在mybatis中實(shí)現(xiàn)擴(kuò)展就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。