dojo/aspect模块是dojo框架中对于AOP的实现。关于AOP的详细解释请读者另行查看其它资料,这里简单复习一下AOP中的基本概念:

  1. 切面(Aspect):其实就是共有功能的实现。如日志切面、权限切面、事务切面等。
  2. 通知(Advice):是切面的具体实现。以目标方法为参照点,根据放置的地方不同,可分为前置通知(Before)、后置通知(After)与环绕通知(Around)。
  3. 连接点(Joinpoint):就是程序在运行过程中能够插入切面的地点。
  4. 目标对象(Target):就是那些即将切入切面的对象,也就是那些被通知的对象。这些对象中已经只剩下干干净净的核心业务逻辑代码了,所有的共有功能代码等待AOP容器的切入。
  5. 代理对象(Proxy):将通知应用到目标对象之后被动态创建的对象。可以简单地理解为,代理对象的功能等于目标对象的核心业务逻辑功能加上共有功能。代理对象对于使用者而言是透明的,是程序运行过程中的产物。
  6. 织入(Weaving):将切面应用到目标对象从而创建一个新的代理对象的过程。这个过程可以发生在编译期、类装载期及运行期,当然不同的发生点有着不同的前提条件。譬如发生在编译期的话,就要求有一个支持这种AOP实现的特殊编译器;发生在类装载期,就要求有一个支持AOP实现的特殊类装载器;只有发生在运行期,则可直接通过Java语言的反射机制与动态代理机制来动态实现。

  生成代理对象的过程可以按照下图理解:

  

  

  dojo/aspect模块代码主要分为两部分:

  • advise方法,通过使用闭包跟链式模型来构造“通知”链。

    "use strict";
    var undefined, nextId = 0;
    function advise(dispatcher, type, advice, receiveArguments){
    var previous = dispatcher[type];
    var around = type == "around";
    var signal;
    if(around){
    var advised = advice(function(){
    return previous.advice(this, arguments);
    });
    signal = {
    remove: function(){
    if(advised){
    advised = dispatcher = advice = null;
    }
    },
    advice: function(target, args){
    return advised ?
    advised.apply(target, args) : // called the advised function
    previous.advice(target, args); // cancelled, skip to next one
    }
    };
    }else{
    // create the remove handler
    signal = {
    remove: function(){
    if(signal.advice){
    var previous = signal.previous;
    var next = signal.next;
    if(!next && !previous){
    delete dispatcher[type];
    }else{
    if(previous){
    previous.next = next;
    }else{
    dispatcher[type] = next;
    }
    if(next){
    next.previous = previous;
    }
    } // remove the advice to signal that this signal has been removed
    dispatcher = advice = signal.advice = null;
    }
    },
    id: nextId++,
    advice: advice,
    receiveArguments: receiveArguments
    };
    }
    if(previous && !around){
    if(type == "after"){
    // add the listener to the end of the list
    // note that we had to change this loop a little bit to workaround a bizarre IE10 JIT bug
    while(previous.next && (previous = previous.next)){}
    previous.next = signal;
    signal.previous = previous;
    }else if(type == "before"){
    // add to beginning
    dispatcher[type] = signal;
    signal.next = previous;
    previous.previous = signal;
    }
    }else{
    // around or first one just replaces
    dispatcher[type] = signal;
    }
    return signal;
    }
  • aspect方法,这个函数返回一个闭包。闭包的作用是将“通知”方法织入到目标函数中,java中运行时通过反射的方式来织入,而js中通过动态更改目标函数来实现织入过程,这时调用该方法可以使切面函数与业务逻辑同时进行。
    function aspect(type){
    return function(target, methodName, advice, receiveArguments){
    var existing = target[methodName], dispatcher;
    if(!existing || existing.target != target){
    // no dispatcher in place
    target[methodName] = dispatcher = function(){
    var executionId = nextId;
    // before advice
    var args = arguments;
    var before = dispatcher.before;
    while(before){
    args = before.advice.apply(this, args) || args;
    before = before.next;
    }
    // around advice
    if(dispatcher.around){
    var results = dispatcher.around.advice(this, args);
    }
    // after advice
    var after = dispatcher.after;
    while(after && after.id < executionId){
    if(after.receiveArguments){
    var newResults = after.advice.apply(this, args);
    // change the return value only if a new value was returned
    results = newResults === undefined ? results : newResults;
    }else{
    results = after.advice.call(this, results, args);
    }
    after = after.next;
    }
    return results;
    };
    if(existing){
    dispatcher.around = {advice: function(target, args){
    return existing.apply(target, args);
    }};
    }
    dispatcher.target = target;
    }
    var results = advise((dispatcher || existing), type, advice, receiveArguments);
    advice = null;
    return results;
    };
    }

  注意:dojo的处理过程中并不生成代理对象,而是直接更改原有的对象的方法。

  关于aspect.after方法(before方法与其类似)的解释请看这篇文章:Javascript事件机制兼容性解决方案;aspect.around的由来在这篇文章Javascript aop(面向切面编程)之around(环绕)里有其一步步的演化过程。

  本文给出aspect模块调用后的示意图:

  before与after函数:

  

  around函数:

  

var advised = advice(function(){
return previous.advice(this, arguments);
});
signal = {
remove: function(){
if(advised){
advised = dispatcher = advice = null;
}
},
advice: function(target, args){
return advised ? //一旦调用remove,adviced变为空,便会跳过本次环绕通知,进入上一层的advice方法。
advised.apply(target, args) : // called the advised function
previous.advice(target, args); // cancelled, skip to next one
}
};

  可以看到around函数中借用闭包形成环绕函数链。这里调用remove方法后并没有像before跟after中将通知方法彻底移除,注册过的环绕方法仍然会存在内存中,所以这个方法无法移除环绕通知,仅仅是避免了在函数链中执行它而已。内存无法释放,不建议使用太多。

dojo/aspect源码解析的更多相关文章

  1. dojo/query源码解析

    dojo/query模块是dojo为开发者提供的dom查询接口.该模块的输出对象是一个使用css选择符来查询dom元素并返回NodeList对象的函数.同时,dojo/query模块也是一个插件,开发 ...

  2. dojo/io-query源码解析

    该模块主要对url中的query部分进行处理,我们发送GET请求时,将参数直接放在URL中,经常碰到的需求就是把一个对象转化为query字符串放到url中去发送GET请求.io-query模块便提供了 ...

  3. dojo Provider(script、xhr、iframe)源码解析

    总体结构 dojo/request/script.dojo/request/xhr.dojo/request/iframe这三者是dojo提供的provider.dojo将内部的所有provider构 ...

  4. 异步任务spring @Async注解源码解析

    1.引子 开启异步任务使用方法: 1).方法上加@Async注解 2).启动类或者配置类上@EnableAsync 2.源码解析 虽然spring5已经出来了,但是我们还是使用的spring4,本文就 ...

  5. Hystrix源码解析

    1. Hystrix源码解析 1.1. @HystrixCommand原理 直接通过Aspect切面来做的 1.2. feign hystrix原理 它的本质原理就是对HystrixCommand的动 ...

  6. spring 源码解析

    1. [文件] spring源码.txt ~ 15B     下载(167) ? 1 springн┤┬вио╬Ш: 2. [文件] spring源码分析之AOP.txt ~ 15KB     下载( ...

  7. 基于注解的SpringAOP源码解析(三)

    注意,读完本篇文章需要很长很长时间 在之前的2篇文章:AOP源码分析(一)AOP源码分析(二) 中,我们搭建了SpringAOP源码分析的环境,介绍了@EnableAspectJAutoProxy注解 ...

  8. Spring系列(五):Spring AOP源码解析

    一.@EnableAspectJAutoProxy注解 在主配置类中添加@EnableAspectJAutoProxy注解,开启aop支持,那么@EnableAspectJAutoProxy到底做了什 ...

  9. Spring系列(六):Spring事务源码解析

    一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...

随机推荐

  1. MVC 前台向后台传输数据

    今天,我们一起来学习下.MVC如何在前台给后台传输数据 (1)前台传输数据到后台 具体思路:前台拼凑json字符串,然后通过 get 或 post 方式,传递到后台 Action 方法中 我现在前台展 ...

  2. C++拷贝构造函数

    拷贝构造函数是一种特殊的构造函数,其定义为第一个参数为为本类型的一个引用或者是常引用,且无其它参数或者其它参数为默认值,例如下面的函数: X::X(const X&); X::X(X& ...

  3. php二维数组按照键值排序的方法

    //按照传入数组中的num倒序 public function numdesc($array,$key="num",$order="desc"){ $arr_n ...

  4. 工程BUG记录

    前天项目中,有个 id 对象,命名成了 initA...  从该页面点击进入其他时没有问题,但是在返回到该页面后,程序崩溃了,报了野指针,后来在跳转传值的时候,发现这个异常.我猜测原因可能是 xcod ...

  5. mvc+mysql+EF

    网上有很多关于EF在联机情况下利用nuget管理器安装的案例,我就讲一下脱机状况吧! 一.建立一个文件夹,例如D:/Packages 放入安装EF和mysql需要的包:EntityFramework. ...

  6. python第二天基础1-1

    一.作用域 对于变量的作用域,执行声明并在内存中存在,该变量就可以在下面的代码中使用. if 1==1: name = 'wupeiqi' print name 二.三元运算 result = 值1  ...

  7. js 有用的代码

    1. 如何创建嵌套的过滤器: //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) //包含class为“s ...

  8. css大小单位px em rem的转换和详解

    css大小单位px em rem的转换和详解 PX特点1. IE无法调整那些使用px作为单位的字体大小:2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位:3. Firefox能 ...

  9. Django学习中的问题总结

    1. 在windows环境下,用pip安装Django后通过在python/Script目录下会生成django-admin.py文件,此时由于pip环境变量已经添加,diango-admin.py与 ...

  10. [LeetCode]436 Find Right Interval

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...