最爱Java

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

《AspectJ Cookbook》读书笔记十五: 定义方面的关系

一.继承切入点定义
        创建一个抽象类。使用合适的public、protected或default访问修饰符在抽象方面内定义可重用的切入点逻辑。最后,把抽象方面继承进子方面中,以重用声明的切入点。

package com.aspectj;

public abstract aspect BasePointcutDefinitionsAspect {
    
public pointcut callPointcut() : call(void MyClass.foo(int,String));
}

 

package com.aspectj;

public aspect ReusePointcutsRecipe extends BasePointcutDefinitionsAspect {
    
//Advice declaration
    before():callPointcut()&&!within(ReusePointcutsRecipe+{
        System.out.println(
"---------- Aspect Advice Logic ----------");
        System.out.println(
"In the advice attached to the call point cut");
        System.out.println(
"Target: " + thisJoinPoint.getTarget());
        System.out.println(
"This: " + thisJoinPoint.getThis());
        System.out.println(
"-----------------------------------------");        
    }

}




二.实现抽象切入点
        在声明切入点和周围的方面时,使用abstract关键字,并且不要提供任何切入点逻辑。

package com.aspectj;

public abstract aspect BaseAbstractAspect {
    
/**
     * Specifies an abstract pointcut placeholder
     * for derived aspects to specify
     
*/

    
public abstract pointcut abstractBasepointcut();
    
    
/**
     * Specifies calling advice whenever a join point
     * picked by the abstractBasePointcut (specified
     * by specialized aspects) is encountered, and not within
     * this aspect or any inheriting aspects.
     
*/

    pointcut runAdvicePointcut() : abstractBasepointcut() 
&& !within(BaseAbstractAspect+);
}

 

package com.aspectj;

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

    
public pointcut abstractBasepointcut():call(void MyClass.foo(int,String));
    
    
//Advice declaration
    before():runAdvicePointcut(){
        System.out.println(
"---------- Aspect Advice Logic ----------");
        System.out.println(
"Signature: " + thisJoinPoint.getStaticPart().getSignature());
        System.out.println(
"Source Location: " + thisJoinPoint.getStaticPart().getSourceLocation());
        System.out.println(
"-----------------------------------------");            
    }


}


三.把类继承进方面中
    使用extends关键字来声明方面扩展类。示例为一个伪日志记录类,它代表一种现有的日志记录机制。其目标是:重构对应用程序中日志记录类的所有现有的调用,并把日志记录模块化进一个方面中,它可以更灵活地织入进应用程序中。

package com.aspectj;

public class OOLogging {
    
public void logEntry(String entry) {
        System.out.println(
"Entry logged: " + entry);
    }

}

 

package com.aspectj;

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

    pointcut callPointcut() : call(
void MyClass.foo(int,String));
    
    
//Advice declaration
    before():callPointcut()&&!within(AOLogging+)&&!within(AOLogging) {
        
this.logEntry(thisJoinPoint.toShortString());
    }

}


 

posted on 2008-08-26 15:34 Brian 阅读(269) 评论(0)  编辑  收藏 所属分类: 《AspectJ Cookbook》读书笔记


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


网站导航:
 

公告


导航

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

统计

常用链接

留言簿(4)

随笔分类

随笔档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜