一.继承切入点定义
        创建一个抽象类。使用合适的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());
    }

}