MyBatis整体预览(二)(六)

2014-11-24 07:43:18 · 作者: · 浏览: 2
terceptsAnnotation.value();

Map, Set> signatureMap = new HashMap, Set>();

for (Signature sig : sigs) {

Set methods = signatureMap.get(sig.type());

if (methods == null) {

methods = new HashSet();

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 Map, Set> 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, Set> signatureMap = new HashMap, Set>();

for (Signature sig : sigs) {

Set methods = signatureMap.get(sig.type());

if (methods == null) {

methods = new HashSet();

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;

}

该方法的第一句话就是获得Intercepts注解,这种方法应该很容易理解。那么接下来将获得在Intercepts里面的参数@Signature注解内容,在该注解中包含三个参数,分别是type,method,args。Type指定要拦截的类对象,method是指明要拦截该类的哪个方法,第三个是指明要拦截的方法参数集合。在Intercepts中可以配置多个@Signature。那么便对这写值进行遍历,已获得对应的type、method以及args。最终是获得一个HashMap对象,这些对象里面的键是类对象,而值是指定的类中方法对象。执行该端程序之后,更具target的classLoader和接口,来创建一个代理,并且,InvocationHandler是创建一个新的Plugin对象,同时将target,interceptor以及signatureMap传递给Plugin对象,当然,这里的Plugin也实现了Invocation接口。那么target对象所有的方法调用都会触发Plugin中的invoke方法,那么这里将执行开发者所有插入的操作。


[java] public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Set 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);
}
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
Set 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);
}
}


发现,此处将判断,复合拦截要求的将执行插件的intercept方法,而在intercept方法里面放入了开发者所要执行的操作。那么此时,就成功的调用了开发者开发的MyBatis的插件了。现在来梳理一下执行的过程。

首先,开发者需要实现MyBatis的Interceptor接口,并主要要实现interceptor和plugin方法,而setProperties()当你配置了property就需要实现,没有,那么可以不用实现。实现接口后,那么就要将插件配置到MyBatis中去,然后通过XMLConfigBuilder来实例化插件对象,并将他们放到Configuration对象的InterceptChain对象的List集合中,然后在Configuration各种ne