Mybatis扩展之Interceptor

Configuration和Interceptor是mybatis中最重要的两个类,Configuration是mybatis中所有数据的载体,包括mybatis配置信息、mapper.xml文件中sql等都存储在Configuration中。而Interceptor则是扩展mybatis功能最重要的接口。

Mybatis通过Interceptor为我们提供了扩展ParameterHandler、ResultSetHandler、StatementHandler、Executor的能力。本章我们介绍Interceptor。首先来看官方给出的example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
java复制代码@Intercepts({
@Signature(type = Map.class, method = "get", args = {Object.class})})
public static class AlwaysMapPlugin implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
return "Always";
}

@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}

@Override
public void setProperties(Properties properties) {
}
}

上面就是Interceptor最简单的使用方法。

使用Interceptor需要了解的5个类:Interceptor、@Intercepts、@Signature、InterceptorChain、Plugin。

Interceptor

Interceptor是开发者需要去实现的接口,主要实现intercept方法,来定义对代理方法的代理逻辑。其接口定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
java复制代码public class Invocation {

private final Object target;
private final Method method;
private final Object[] args;
}
public interface Interceptor {

//对代理方法进行逻辑扩展
Object intercept(Invocation invocation) throws Throwable;

//对指定对象生成代理类,一般直接使用org.apache.ibatis.plugin.Plugin#wrap,也可以自己实现代理逻辑
Object plugin(Object target);

//设置Properties参数
void setProperties(Properties properties);

}

@Intercepts、@Signature

注解在Interceptor实现类上,用来指定需要被代理的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
java复制代码@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Intercepts {
Signature[] value();
}

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({})
public @interface Signature {
/**
* 需要对哪些类进行代理
* @return
*/
Class<?> type();

/**
* 需要对类的哪些方法进行代理
* @return
*/
String method();

/**
* 配合method()对需要代理的方法进行限定
* @return
*/
Class<?>[] args();
}

需要说明的是虽然上面例子实现了对Map类的代理,但是在Mybatis中一般只对ParameterHandler、ResultSetHandler、StatementHandler、Executor这4个类进行代理。

Plugin

Mybatis提供的默认代理工厂类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
java复制代码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);
}
}

private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);
// issue #251
if (interceptsAnnotation == null) {
throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
}
Signature[] sigs = interceptsAnnotation.value();
Map<Class<?>, Set<Method>> signatureMap = new HashMap<>();
for (Signature sig : sigs) {
Set<Method> methods = signatureMap.computeIfAbsent(sig.type(), k -> new HashSet<>());
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 Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) {
Set<Class<?>> interfaces = new HashSet<>();
while (type != null) {
for (Class<?> c : type.getInterfaces()) {
if (signatureMap.containsKey(c)) {
interfaces.add(c);
}
}
type = type.getSuperclass();
}
return interfaces.toArray(new Class<?>[interfaces.size()]);
}

InterceptorChain

用来存储多个Interceptor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
java复制代码public class InterceptorChain {

private final List<Interceptor> interceptors = new ArrayList<>();

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);
}

}

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%