最爱Java

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

《AspectJ Cookbook》读书笔记九: 捕获程序作用域内的连接点

    切入点定义设计中的常用方式是:基于关注的程序作用域,限制捕获连接点的范围。这可以让你即使控制在进一步的切入点定义中将会涉及哪些连接点。
    本章中的切入点相当容易理解,并且他们是AspectJ中一些常用的元素。例如,广泛使用的within(TypePattern)切入点将以!within(%THIS_ASPECT%)形式使用它。这个AspectJ术语限制了当前方面之外的每个连接点的作用域,从而可以防止通知触发对相同通知块的递归调用,并导致一个无限循环。
    一. 捕获特定类中的所有连接点
    使用within(TypePattern)切入点,利用TypePattern指定特定类类型模式,within(TypePattern)切入点的语法如下:
    pointcut <pointcut name>(<any values to be picked up>) : within(<class>);

    within(TypePattern)切入点具有3个关键特征:
        1.within(TypePattern)切入点捕获指定类作用域中的所有连接点。
        2.within(TypePattern)切入点极少孤立使用。相反,它通常与切入点结合使用,用于减少将触发附带通知的连接点。
        3.TypePattern可以包含通配符,用于选择不同类上的一系列连接点。


 

 

package com.aspectj;

public aspect WithinClassRecipe {
    
/**
     * Specifies calling advice on any join point encountered within
     * the defined scope:
     * 
     * Scope:MyClass
     
*/

    pointcut withinMyClass() : within(MyClass);
    
    
//Advice declaration
    before() : withinMyClass() && !within(WithinClassRecipe+{
        System.out.println(
"---------- Aspect Advice Logic ----------");
        System.out.println(
"In the advice picked by withinMyClass()");
        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(
"-----------------------------------------");
    }


}



二. 捕获特定包中的所有连接点
    within(TypePattern)切入点提供一种有用的手段,它使用通配符在出现在每个类中的连接点中指定一个关注。可以在TypePattern中使用合适的通配符,从切入点逻辑的余下部分中包含或排除连接点的整个包。

package com.aspectj;

public aspect WithinPackageRecipe {
    
/**
     * Specifies calling advice on any join point encountered within
     * the defined scope:
     * 
     * Scope:packageA
     
*/

    pointcut withinPackageA() : within(packageA.
*);
    
    
//Advice declaration
    before() : withinPackageA() && !within(WithinPackageRecipe+{
        System.out.println(
"---------- Aspect Advice Logic ----------");
        System.out.println(
"In the advice picked by WitinPackageRecipe()");
        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(
"-----------------------------------------");
    }


}


 

三. 捕获特定方法内的所有连接点
    使用withincode(Signature)切入点。withincode(Signature)切入点的语法如下:
    pointcut <pointcut name>(<any values to be picked up>) : withincode(<modifier> <class>.<method>(<paramter types>));

    withincode(Signature)切入点有3个关键特征:
        1. withincode(Signature)切入点指定了特定方法的本地作用域内的所有连接点。
        2. withincode(Signature)切入点极少孤立使用。相反,他通常与其他切入点结合使用,用于减少将触发附带通知的连接点。
        3. Signaure可以包含通配符,用于选择不同类的不同方法上的一系列连接点。

package com.aspectj;

public aspect WithinMethodRecipe {
    
/**
     * Specifies calling advice whenever a method
     * matching the following rules gets called:
     * 
     * Class Name:MyClass
     * Method Name:foo
     * Method Return Type: * (any return type)
     * Method Parameters: an int followed by a String
     
*/

    pointcut withinFooIntStringAnyReturnPointcut() : withincode(
* MyClass.foo(int,String));
    
    
//Advice declaration
    before() : withinFooIntStringAnyReturnPointcut() && !within(WithinMethodRecipe+{
        System.out.println(
"---------- Aspect Advice Logic ----------");
        System.out.println(
"In the advice picked by withinMyClass()");
        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(
"-----------------------------------------");
    }


}

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


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


网站导航:
 

公告


导航

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

统计

常用链接

留言簿(4)

随笔分类

随笔档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜