加入测试代码
public static void main(String[] args) throws IOException {
String resource = "conf.xml";
InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = sessionFactory.openSession();
// User user = session.selectOne("weber.mybatis.mapper.getUser", 1);
User userObj = new User();
userObj.setId(123);
session.selectList("weber.mybatis.mapper.searchByUser", userObj);
// UserMapper mapper = session.getMapper(UserMapper.class);
// mapper.selectAll();
// System.out.println(user);
}
开启debug模式,我们将看到Plugin是如何实现代理的.
当代码执行到下图红色部分的时候
将直接跳到下图!所以,我们此时的executor不是CachingExecutor对象,而是Plugin代理对象.
此时的method,就是被调用的目标方法如下:
最后附上源码注释
/**
* @author Clinton Begin
*/
//Plugin是JDK动态代理类
public class Plugin implements InvocationHandler {
private Object target;//目标对象(ParameterHandler,ResultSetHandler,StatementHandler,Executor)
private Interceptor interceptor;//被代理的拦截器
//目标类需要拦截的方法缓存.因为一个拦截器可以拦截多个类,一个类可以拦截多个方法.
//所以用Map + Set的数据结构存储
private Map, Set> signatureMap;//保存每个拦截器的@signature的配置信息
private Plugin(Object target, Interceptor interceptor, Map, Set> signatureMap) {
this.target = target;
this.interceptor = interceptor;
this.signatureMap = signatureMap;
}
//把目标对象和拦截器封装成Plugin代理类实例.
public static Object wrap(Object target, Interceptor interceptor) {
Map, Set> signatureMap = getSignatureMap(interceptor);//获取拦截器的拦截信息(需要拦截的类和方法)
Class> type = target.getClass();
Class>[] interfaces = getAllInterfaces(type, signatureMap);//Proxy代理只能代理接口
if (interfaces.length > 0) {
return Proxy.newProxyInstance(
type.getClassLoader(),
interfaces,
new Plugin(target, interceptor, signatureMap));//Plugin作为代理类,但是实际业务是由Interceptor拦截器完成的.
}
return target;
}
@Override
//proxy,类代理的对象,例如CachingExecutor对象
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {//从这里代码看到,会拦截所有的Executor方法,动态的去判断拦截器要不要去拦截.所以要小心使用拦截器,会影响性能.
Set methods = signatureMap.get(method.getDeclaringClass());
if (methods != null && methods.contains(method)) {
//Invocation是目标对象,目标对象需要拦截的方法,我拦截方法的参数的封装.
return interceptor.intercept(new Invocation(target, method, args));//调用拦截器实现拦截
}
return method.invoke(target, args);//不需要拦截的方法直接放行
} catch (Exception e) {
throw ExceptionUtil.unwrapThrowable(e);
}
}
private static Map, Set> getSignatureMap(Interceptor interceptor) {
Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);//获取拦截器注解@Signature
// issue #251
if (interceptsAnnotation == null) {
throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());
}
Signature[] sigs = interceptsAnnotation.value();//一个Signature表示一个拦截类型
//保存需要拦截类的信息,class作为key, 需要拦截类的方法作为value集合Set保存.一个拦截器可以拦截一个类中多个方法
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 Class>[] getAllInterfaces(Class> type, Map, Set> signatureMap) {
Set> 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()]);
}
}
/**
* @author Clinton Begin
*/
//InterceptorChain里保存了所有的拦截器,它在mybatis初始化的时候创建。存在Configuration中
public class InterceptorChain {
private final List 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 getInterceptors() {
return Collections.unmodifiableList(interceptors);
}
}
/**
* @author Clinton Begin
*/
public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable;//拦截方法,在这里处理拦截器的业务逻辑
Object plugin(Object target);//把目标对象封装成Plugin对象
void setProperties(Properties properties);
}