>  기사  >  Java  >  [MyBatis 소스코드 분석] 플러그인 구현 원리

[MyBatis 소스코드 분석] 플러그인 구현 원리

巴扎黑
巴扎黑원래의
2017-06-26 09:35:151110검색

MyBatis 플러그인 원리 ----<플러그인>

MyBatis에 대해 잘 모르는 분들을 위해 그 전에 MyBatis의 플러그인 구현 원리를 분석한 글입니다. 플러그인에 대한 자세한 내용은 MyBatis7: MyBatis 플러그인 및 예제를 참조하세요. --각 SQL 문과 해당 실행 시간을 인쇄합니다. 이 문서에서는 예제를 사용하여 MyBatis 플러그인이 무엇인지, 어떻게 작동하는지 설명합니다. 그것을 구현하기 위해. MyBatis 플러그인은 MyBatis의 기본 코드에 침투했기 때문에 플러그인을 더 잘 사용하려면 플러그인 구현 원리와 MyBatis의 기본 코드를 잘 알고 있어야 합니다. 이 기사에서는 플러그인 구현 원리를 분석합니다. 마이바티스.

먼저, 플러그인 의 구문 분석부터 시작합니다. 소스 코드는 XMLConfigBuilder의pluginElement 메소드에 있습니다.

 1 private void pluginElement(XNode parent) throws Exception { 2     if (parent != null) { 3       for (XNode child : parent.getChildren()) { 4         String interceptor = child.getStringAttribute("interceptor"); 5         Properties properties = child.getChildrenAsProperties(); 6         Interceptor interceptorInstance = (Interceptor) resolveClass(interceptor).newInstance(); 7         interceptorInstance.setProperties(properties); 8         configuration.addInterceptor(interceptorInstance); 9       }10     }11 }

여기 태그의 인터셉터 속성이 있습니다. , 이는 사용자 정의 인터셉터입니다. 전체 경로인 6행의 코드는 리플렉션을 통해 인터셉터 인스턴스를 생성합니다.

태그 아래의 모든 태그를 가져와서 이름 및 값 속성을 속성으로 구문 분석하고 속성을 인터셉터로 설정합니다.

마지막으로 8번째 줄의 코드를 통해 인터셉터를 Configuration으로 설정합니다. 소스 코드는 다음과 같이 구현됩니다.

<span style="color: #008080"> 1</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> addInterceptor(Interceptor interceptor) {</span><span style="color: #008080"> 2</span> <span style="color: #000000">    interceptorChain.addInterceptor(interceptor);</span><span style="color: #008080"> 3</span> <span style="color: #000000">}</span><span style="color: #008080"><br></span>

InterceptorChain은 정의된 모든 인터셉터와 여러 관련 인터셉터를 저장하는 인터셉터 체인입니다. 작업 방법:

 1 public class InterceptorChain { 2  3   private final List<Interceptor> interceptors = new ArrayList<Interceptor>(); 4  5   public Object pluginAll(Object target) { 6     for (Interceptor interceptor : interceptors) { 7       target = interceptor.plugin(target); 8     } 9     return target;10   }11 12   public void addInterceptor(Interceptor interceptor) {13     interceptors.add(interceptor);14   }15   16   public List<Interceptor> getInterceptors() {17     return Collections.unmodifiableList(interceptors);18   }19 20 }

세 가지 방법이 있습니다: 인터셉터 추가, 대상 객체에 모든 인터셉터 추가, 현재 모든 인터셉터 가져오기.

MyBatis 플러그인 원리---pluginAll 메소드는 플러그인을 추가합니다

위에서 InterceptorChain의pluginAll 메소드를 보았습니다. pluginAll 메소드는 대상 객체에 대한 프록시를 생성한 다음 대상 객체가 해당 메소드를 호출합니다. 이때는 원래의 메소드가 아닌 나중에 설명할 프록시 메소드입니다.

MyBatis 공식 웹사이트 문서에는 플러그인을 다음 네 가지 코드 실행 지점에서 사용할 수 있다고 명시되어 있습니다. 즉, Timing

이라고 불리는 PluginAll 메소드는 Executor, ParameterHandler, ResultSetHandler, StatementHandler의 4가지 인터페이스 구현 클래스가 생성되는 시점이다. MyBatis에서 생성되는 각 인터페이스 구현 클래스의 타이밍은 시기에 따라 다르지 않다. 내가 사용하는 Eclipse는 Ctrl+Alt+H입니다.

pluginAll 메소드를 다시 살펴보세요:

1 public Object pluginAll(Object target) {2     for (Interceptor interceptor : interceptors) {3       target = interceptor.plugin(target);4     }5     return target;6 }

这里值得注意的是:

  1. 形参Object target,这个是Executor、ParameterHandler、ResultSetHandler、StatementHandler接口的实现类,换句话说,plugin方法是要为Executor、ParameterHandler、ResultSetHandler、StatementHandler的实现类生成代理,从而在调用这几个类的方法的时候,其实调用的是InvocationHandler的invoke方法

  2. 这里的target是通过for循环不断赋值的,也就是说如果有多个拦截器,那么如果我用P表示代理,生成第一次代理为P(target),生成第二次代理为P(P(target)),生成第三次代理为P(P(P(target))),不断嵌套下去,这就得到一个重要的结论:...中后定义的实际其拦截器方法先被执行,因为根据这段代码来看,后定义的代理实际后生成,包装了先生成的代理,自然其代理方法也先执行

plugin方法中调用MyBatis提供的现成的生成代理的方法Plugin.wrap(Object target, Interceptor interceptor),接着我们看下wrap方法的源码实现。

 

MyBatis插件原理----Plugin的wrap方法的实现

Plugin的wrap方法实现为:

 1 public static Object wrap(Object target, Interceptor interceptor) { 2     Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor); 3     Class<?> type = target.getClass(); 4     Class<?>[] interfaces = getAllInterfaces(type, signatureMap); 5     if (interfaces.length > 0) { 6       return Proxy.newProxyInstance( 7           type.getClassLoader(), 8           interfaces, 9           new Plugin(target, interceptor, signatureMap));10     }11     return target;12 }

首先看一下第2行的代码,获取Interceptor上定义的所有方法签名:

 1 private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) { 2     Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class); 3     // issue #251 4     if (interceptsAnnotation == null) { 5       throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());      
 6     } 7     Signature[] sigs = interceptsAnnotation.value(); 8     Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>(); 9     for (Signature sig : sigs) {10       Set<Method> methods = signatureMap.get(sig.type());11       if (methods == null) {12         methods = new HashSet<Method>();13         signatureMap.put(sig.type(), methods);14       }15       try {16         Method method = sig.type().getMethod(sig.method(), sig.args());17         methods.add(method);18       } catch (NoSuchMethodException e) {19         throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);20       }21     }22     return signatureMap;23 }

看到先拿@Intercepts注解,如果没有定义@Intercepts注解,抛出异常,这意味着使用MyBatis的插件,必须使用注解方式

接着拿到@Intercepts注解下的所有@Signature注解,获取其type属性(表示具体某个接口),再根据method与args两个属性去type下找方法签名一致的方法Method(如果没有方法签名一致的就抛出异常,此签名的方法在该接口下找不到),能找到的话key=type,value=Set,添加到signatureMap中,构建出一个方法签名映射。举个例子来说,就是我定义的@Intercepts注解,Executor下我要拦截的所有Method、StatementHandler下我要拦截的所有Method。

回过头继续看wrap方法,在拿到方法签名映射后,调用getAllInterfaces方法,传入的是Target的Class对象以及之前获取到的方法签名映射:

 1 private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) { 2     Set<Class<?>> interfaces = new HashSet<Class<?>>(); 3     while (type != null) { 4       for (Class<?> c : type.getInterfaces()) { 5         if (signatureMap.containsKey(c)) { 6           interfaces.add(c); 7         } 8       } 9       type = type.getSuperclass();10     }11     return interfaces.toArray(new Class<?>[interfaces.size()]);12 }

这里获取Target的所有接口,如果方法签名映射中有这个接口,那么添加到interfaces中,这是一个Set,最终将Set转换为数组返回。

wrap方法的最后一步:

1 if (interfaces.length > 0) {2   return Proxy.newProxyInstance(3       type.getClassLoader(),4       interfaces,5       new Plugin(target, interceptor, signatureMap));6 }7 return target;

如果当前传入的Target的接口中有@Intercepts注解中定义的接口,那么为之生成代理,否则原Target返回。

这段理论可能大家会看得有点云里雾里,我这里举个例子:

= StatementHandler., method = "query", args = {Statement., ResultHandler.= StatementHandler., 
method = "update", args = {Statement. org.apache.ibatis.executor.statement.StatementHandler=[   org.apache.ibatis.executor.statement.StatementHandler.update(java.sql.
Statement)  java.sql.SQLException,   java.util.List org.apache.ibatis.executor.statement.StatementHandler.query(java.sql.Statement,org.apache.
ibatis.session.ResultHandler)  java.sql.SQLException]}
一个Class对应一个Set,Class为StatementHandler.class,Set为StataementHandler中的两个方法

如果我new的是StatementHandler接口的实现类,那么可以为之生成代理,因为signatureMap中的key有StatementHandler这个接口

如果我new的是Executor接口的实现类,那么直接会把Executor接口的实现类原样返回,因为signatureMap中的key并没有Executor这个接口

相信这么解释大家应该会明白一点。注意这里生不生成代理,只和接口在不在@Intercepts中定义过有关,和方法签名无关,具体某个方法走拦截器,在invoke方法中,马上来看一下。

 

MyBatis插件原理----Plugin的invoke方法

首先看一下Plugin方法的方法定义:

 1 public class Plugin implements InvocationHandler { 2  3   private Object target; 4   private Interceptor interceptor; 5   private Map<Class<?>, Set<Method>> signatureMap; 6  7   private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) { 8     this.target = target; 9     this.interceptor = interceptor;10     this.signatureMap = signatureMap;11   }12   ...13 }

看到Plugin是InvocationHandler接口的实现类,换句话说,为目标接口生成代理之后,最终执行的都是Plugin的invoke方法,看一下invoke方法的实现:

 1 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 2     try { 3       Set<Method> methods = signatureMap.get(method.getDeclaringClass()); 4       if (methods != null && methods.contains(method)) { 5         return interceptor.intercept(new Invocation(target, method, args)); 6       } 7       return method.invoke(target, args); 8     } catch (Exception e) { 9       throw ExceptionUtil.unwrapThrowable(e);10     }11 }

여기서 해당 메소드에 해당하는 Class를 꺼내서 Class에서 메소드 시그니처를 얻습니다. 즉, Executor, ParameterHandler, ResultSetHandler, 그리고 StateHandler는 어떤 메소드 시그니처를 가로챌 것인지 정의합니다.

현재 호출된 메소드의 메소드 시그니처가 메소드 시그니처 세트에 있으면, 즉 4행의 판단을 충족하면 인터셉터의 인터셉트 메소드가 호출되고, 그렇지 않으면 해당 메소드가 그대로 호출됩니다. 인터셉터는 실행되지 않습니다.

위 내용은 [MyBatis 소스코드 분석] 플러그인 구현 원리의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.