最爱Java

书山有路勤为径,学海无涯苦作舟

《AspectJ Cookbook》读书笔记十: 捕获基于控制流程的连接点

    本章中描述的切入点支持捕获另一个初始连接点作用域或环境内的所有连接点。每个连接点在程序的控制流程中都有一个具体位置,这为通过这里描述的切入点声明捕获的连接点提供了环境。
    一. 捕获通过初始连接点开始的程序控制流程内的所有连接点
    使用cflow(Pointcut)切入点。cflow(Pointcut)切入点的语法如下:
    pointcut <pointcut name>(<any values to be picked up>) : cflow(<pointcut>);

    cflow(Pointcut)切入点具有3个关键特征:
        1.cflow(Pointcut)切入点捕获在初始特定的连接点环境内遇到的所有连接点,这个初始连接点是通过另一个切入点选择的。
        2.捕获的连接点包括初始连接点。
        3.作用域是cflow(pointcut)切入点中重要的鉴别器。这个切入点将捕获通过切入点参数捕获的连接点的控制流程内的所有连接点。


package com.aspectj;

public aspect CFlowRecipe {
    
/**
     * Specifies calling advice whenever a method
     * matching the following rules gets called:
     * 
     * Class Name:MyClass
     * Method Name:foo
     * Method Return Type:void
     * Method Parameters: an int followed by a String
     
*/

    pointcut callInitialPointcut():call(
void MyClass.foo(int,String));
    
    
/**
     * Specifies calling advice whenever a join point is encountered
     * including and after the initial join point triggers the pointcut
     * that is specified in the parameter:
     * 
     * Pointcut Name:callInitialPointcut
     
*/

    pointcut cflowPointcut():cflow(callInitialPointcut());
    
    
//Advice declaration
    before() : cflowPointcut() && !within(CFlowRecipe+{
        System.out.println(
"---------- Aspect Advice Logic ----------");
        System.out.println(
"In the advice picked by CFlowRecipe()");
        System.out.println(
"Join Point Kind: " + thisJoinPoint.getKind());
        System.out.println(
"Signature: " + thisJoinPoint.getStaticPart().getSignature());
        System.out.println(
"Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
        System.out.println(
"-----------------------------------------");
    }
    
    

}



        值得更详细研究cflow(Pointcut)做什么。这个特殊的切入点引入了连接点环境的概念。它是指每个连接点具有一个作用域,在这个用途域内,它被看成是执行程序的控制流程的一部分。
        在这个控制流程内,任何遇到的连接点都会触发cflow(Pointcut)切入点,并调用任何关联的通知。当初始连接点触发指定的切入点参数时,cflow(Pointcut)切入点会起作用,并触发其关联的通知。然后,将为在初始连接点环境内的控制流程中遇到的每个连接点调用与cflow(Pointcut)关联的通知。最后,捕获的连接点集合包括初始连接点本身,这就是这个切入点与cflowbelow(Pointcut)切入点之间的主要区别。

        在cflow(Pointcut)的当前实现中,在使用它时,其实现方式会引入大量的系统开销。在可能的地方,并且连接点重用不受影响时,可以考虑优先使用withincode(Signature)切入点。

    二.捕获程序控制流程内的所有连接点,不包括初始连接点
    使用cflowbelow(Pointcut)切入点。cflowbelow(Pointcut)切入点的语法如下:
    pointcut <pointcut name>(<any values to be picked up>) : cflowbelow(<pointcut>);


package com.aspectj;

public aspect CFlowBelowRecipe {
    
/**
     * Specifies calling advice whenever a method
     * matching the following rules gets called:
     * 
     * Class Name:MyClass
     * Method Name:foo
     * Method Return Type:void
     * Method Parameters: an int followed by a String
     
*/

    pointcut callInitialPointcut():call(
void MyClass.foo(int,String));
    
    
/**
     * Specifies calling advice whenever a join point is encountered
     * after the initial join point triggers the pointcut
     * that is specified in the parameter:
     * 
     * Pointcut Name:callInitialPointcut
     
*/

    pointcut cflowPointcut():cflowbelow(callInitialPointcut());
    
    
//Advice declaration
    before() : cflowPointcut() && !within(CFlowBelowRecipe+{
        System.out.println(
"---------- Aspect Advice Logic ----------");
        System.out.println(
"In the advice picked by CFlowBelowRecipe()");
        System.out.println(
"Join Point Kind: " + thisJoinPoint.getKind());
        System.out.println(
"Signature: " + thisJoinPoint.getStaticPart().getSignature());
        System.out.println(
"Source Line: " + thisJoinPoint.getStaticPart().getSourceLocation());
        System.out.println(
"-----------------------------------------");
    }
    
    

}



    这里和第一部分介绍的内容有点区别;其区别是实际捕获的连接点数量。cflow(Pointcut)切入点会触发在初始连接点环境内遇到的所有连接点(包括初始连接点)上的通知,而cflowbelow(Pointcut)切入点则不包括那个初始连接点。

posted on 2008-08-25 10:36 Brian 阅读(416) 评论(0)  编辑  收藏 所属分类: 《AspectJ Cookbook》读书笔记


只有注册用户登录后才能发表评论。


网站导航:
 

公告


导航

<2008年8月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(4)

随笔分类

随笔档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜