dojo/aspect源码解析
dojo/aspect模块是dojo框架中对于AOP的实现。关于AOP的详细解释请读者另行查看其它资料,这里简单复习一下AOP中的基本概念:
- 切面(Aspect):其实就是共有功能的实现。如日志切面、权限切面、事务切面等。
- 通知(Advice):是切面的具体实现。以目标方法为参照点,根据放置的地方不同,可分为前置通知(Before)、后置通知(After)与环绕通知(Around)。
- 连接点(Joinpoint):就是程序在运行过程中能够插入切面的地点。
- 目标对象(Target):就是那些即将切入切面的对象,也就是那些被通知的对象。这些对象中已经只剩下干干净净的核心业务逻辑代码了,所有的共有功能代码等待AOP容器的切入。
- 代理对象(Proxy):将通知应用到目标对象之后被动态创建的对象。可以简单地理解为,代理对象的功能等于目标对象的核心业务逻辑功能加上共有功能。代理对象对于使用者而言是透明的,是程序运行过程中的产物。
- 织入(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源码解析的更多相关文章
- dojo/query源码解析
dojo/query模块是dojo为开发者提供的dom查询接口.该模块的输出对象是一个使用css选择符来查询dom元素并返回NodeList对象的函数.同时,dojo/query模块也是一个插件,开发 ...
- dojo/io-query源码解析
该模块主要对url中的query部分进行处理,我们发送GET请求时,将参数直接放在URL中,经常碰到的需求就是把一个对象转化为query字符串放到url中去发送GET请求.io-query模块便提供了 ...
- dojo Provider(script、xhr、iframe)源码解析
总体结构 dojo/request/script.dojo/request/xhr.dojo/request/iframe这三者是dojo提供的provider.dojo将内部的所有provider构 ...
- 异步任务spring @Async注解源码解析
1.引子 开启异步任务使用方法: 1).方法上加@Async注解 2).启动类或者配置类上@EnableAsync 2.源码解析 虽然spring5已经出来了,但是我们还是使用的spring4,本文就 ...
- Hystrix源码解析
1. Hystrix源码解析 1.1. @HystrixCommand原理 直接通过Aspect切面来做的 1.2. feign hystrix原理 它的本质原理就是对HystrixCommand的动 ...
- spring 源码解析
1. [文件] spring源码.txt ~ 15B 下载(167) ? 1 springн┤┬вио╬Ш: 2. [文件] spring源码分析之AOP.txt ~ 15KB 下载( ...
- 基于注解的SpringAOP源码解析(三)
注意,读完本篇文章需要很长很长时间 在之前的2篇文章:AOP源码分析(一)AOP源码分析(二) 中,我们搭建了SpringAOP源码分析的环境,介绍了@EnableAspectJAutoProxy注解 ...
- Spring系列(五):Spring AOP源码解析
一.@EnableAspectJAutoProxy注解 在主配置类中添加@EnableAspectJAutoProxy注解,开启aop支持,那么@EnableAspectJAutoProxy到底做了什 ...
- Spring系列(六):Spring事务源码解析
一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...
随机推荐
- easy ui 零散技巧
1.Jquery带上下文查找: 格式:$(selector,context) 例如:$("input",window.document),查找当前文档下的说有input元素,也等价 ...
- SVN服务器的配置(简单易懂,带配置文件,有注释)
这两天在服务器搭建了一个SVN服务器,一些经验,也留作后用把,有不详细的欢迎批评指正 另外关于子目录的访问配置,这块我还是不懂,希望有前辈能教我一下 1.安装SVN Serveryum install ...
- linux下搭建nagios
配置环境:1)CentOS 6.5 作为监控主机,IP:10.0.0.30(根据自己公司需要改变) 2)客户机: windows server 2008R2 , windows 7, windows ...
- 理解Java Integer的缓存策略
转载自http://www.importnew.com/18884.html 本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的一个有助于节省内存.提高性能的特性. ...
- vs调试断点进不去的解决办法
原创文章,禁止转载. 断点进不去的解决办法: 确认是debug版本 确认生成了调试信息 确认在编译和连接的工程配置中指定了相同的匹配的pdb文件名,而不是默认的vc100.pdb等名字(无关) ...
- Java学习笔记 07 接口、继承与多态
一.类的继承 继承的好处 >>使整个程序架构具有一定的弹性,在程序中复用一些已经定义完善的类不仅可以减少软件开发周期,也可以提高软件的可维护性和可扩展性 继承的基本思想 >>基 ...
- Spring 学习笔记 3. 尚硅谷_佟刚_Spring_配置 Bean
1,bean 的配置 <bean id="helloWorld" class="com.yfy.HelloWorld"> <property ...
- java加密解压类库
java压缩类库_支持加密解压.zip public static void unzip(File zipFile, String dest, String passwd) throws ZipExc ...
- 《python核心编程》笔记——杂项
python语句默认会给每一行添加一个换行符,只要在最后加一个逗号就能改变这种行为 若函数里没有return就自动返回None对象 PEP(python增强提案简称)http://python.org ...
- linux shell
1.+到n for i in {1..n}doa=$(($a+$i))doneecho $a 2. 写一个脚本.输入如下效果 0 01 012 0123 01234 012345 0123456 01 ...