设为首页 收藏本站
查看: 1169|回复: 0

[经验分享] MyBatis之拦截器interceptor代码赏析

[复制链接]

尚未签到

发表于 2016-11-28 06:34:11 | 显示全部楼层 |阅读模式
  拦截器已经是各个开源软件必不可少的功能。 在讨论各种问题的时候也经常听说这个对象被拦截了等等。那么在JAVA的世界里, 是怎么实现拦截器的功能的呢 ?  要了解这些, 必须先从代理类(Proxy)说起,但是我们在这里不打算从这里介绍,我们直接上Mybatis的测试代码。

public class PluginTest {
@Test
public void mapPluginShouldInterceptGet() {
Map map = new HashMap();
map = (Map) new AlwaysMapPlugin().plugin(map);
assertEquals("Always", map.get("Anything"));
}
@Test
public void shouldNotInterceptToString() {
Map map = new HashMap();
map = (Map) new AlwaysMapPlugin().plugin(map);
assertFalse("Always".equals(map.toString()));
}
@Intercepts({
@Signature(type = Map.class, method = "get", args = {Object.class})})
public static class AlwaysMapPlugin implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
return "Always";
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}
}
  MyBatis 直接抽象了一个plugin的概念,中文意思就是“插头”吧,很形象。我要对一个对象拦截,我就要把这个插头插入到目标对象中:

map = (Map) new AlwaysMapPlugin().plugin(map);
  这里生成了一个AlwaysMapPlugin,并调用plugin方法,把自己插入到Map的一个实例对象中;我们先不管plugin方法里面到底做了什么,我们只要知道它还是给我返回了一个Map的实例。

assertEquals("Always", map.get("Anything"));
  接着我们就调用了Map实例的get方法,我们纳闷了,我们并没有给Map实例中添加任何数据,但是却能得到"Always”。
  很神奇吧...
  要解开这个神奇,还是要回到AlwaysMapPlugin().plugin(Object object)的方法上来。

public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
  直接委托给了Plugin类的一个静态方法:

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;
}
  我们先抛开乱七八糟的逻辑,我们看到在满足一定的条件的情况下,会返回一个Proxy.newProxyInstance类的一个实例。也就是说AlwaysMapPlugin().plugin(Object object)的方法返回的实例,是有可能和原来的实例不是同一个,而是原来实例的一个代理类。

public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
  这个是Proxy.newProxyInstance方法的签名,不明白的同志自己去了解下。
  我们已经得到了目标对象的代理类,可是我们只想对目标对象的某几个方法进行拦截, 是怎么做到的呢?
  这次我们把目光转向我们的“插头”吧。

@Intercepts({
@Signature(type = Map.class, method = "get", args = {Object.class})})
public static class AlwaysMapPlugin implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
return "Always";
}
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
}
}
  这里通过注解告诉我们,会对Map.get(Object obj)的方法进行拦截。而且在拦截之后,直接返回了Always, 这就是最终的原因了。
  罗里吧嗦一大堆,我们直接看MyBatis里面抽象的几个相关类吧:
  1.  Plugin  类:也就是我们说的“插板”类, 继承了InvocationHandler 接口,主要的职责是将目标对象、拦截器组装成一个新的代理类。 简单的代码如下:

public class Plugin implements InvocationHandler {
private Object target; // 目标对象
private Interceptor interceptor; //拦截器对象
private 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;
}
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);
}
}
  2. Interceptor 拦截器的接口:

public interface Interceptor {
Object intercept(Invocation invocation) throws Throwable; // 对方法进行拦截的抽象方法
Object plugin(Object target); //把拦截器插入到目标对象的方法
void setProperties(Properties properties);
}
  3. Invocation 真正对目标类方法的拦截的实现, 这里没有什么说的。 值得一提的是, 如果我们想实现类似spring的拦截器,比如说前置通知、后置通知、环绕通知等,应该是可以在这里做文章的。

public class Invocation {
private Object target;
private Method method;
private Object[] args;
public Invocation(Object target, Method method, Object[] args) {
this.target = target;
this.method = method;
this.args = args;
}
public Object getTarget() {
return target;
}
public Method getMethod() {
return method;
}
public Object[] getArgs() {
return args;
}
public Object proceed() throws InvocationTargetException, IllegalAccessException {
return method.invoke(target, args);
}
}
  4. 搞定。
  总结:其实拦截器还是很简单的, 只需要熟识了Proxy类、InvocationHandler接口, 基本都能写出来,只是写的好与坏,是不是低耦合、高内聚的代码。类的命名是不是让别人一看就懂,这些都决定着一个人水平的高低;
  拦截器使用的场景: 比如说 权限、日志记录、缓存等等;
  先分享到这里.....

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306281-1-1.html 上篇帖子: spring整合mybatis之后部分log日志配置失效 下篇帖子: Mybatis SQL映射语句中参数注释规则
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表