关于aop:如何从Aspectj中排除方法

How to exclude methods from aspectj

我正在尝试使用Aspectj从日志文件中排除几种方法(即使用spring和Load-time weaving)。有没有办法在aop.xml中列出排除的方法?我知道我可以为完整课程做这件事,但是我正在寻找特定的方法。或者我可以在方面类中列出清单?
谢谢


我不知道如何在XML中执行此操作,但是在切面本身中很容易做到,因为切入点可以使用布尔运算符进行组合。

传统aspectj语法:

1
2
3
pointcut whatIDontWantToMatch() : within(SomeClass+) || execution(* @SomeAnnotation *.*(..));
pointcut whatIWantToMatch()     : execution(* some.pattern.here.*(..));
pointcut allIWantToMatch()      : whatIWantToMatch() && ! whatIDontWantToMatch();

@AspectJ语法:

1
2
3
4
5
6
@Pointcut("within(SomeClass+) || execution(* @SomeAnnotation *.*(..))")
public void whatIDontWantToMatch(){}
@Pointcut("execution(* some.pattern.here.*(..))")
public void whatIWantToMatch(){}
@Pointcut("whatIWantToMatch() && ! whatIDontWantToMatch()")
public void allIWantToMatch(){}

这些当然只是示例。 whatIDontWantToMatch()也可以由多个切入点等组成。


这是在Aspectj中排除基于aop.xml或aop-ajc.xml的方法的方法

1
2
3
4
5
6
7
8
9
    <concrete-aspect name="com.perf.aspects.ConcreteAspectEJBInclude" extends="com.perf.aspects.EJBAspect">
    <pointcut name="ejbPointCutInclude"
        expression="execution(* com.perf.test.TestClass..*(..))" />
       <!-- Methods to exclude for the above execution methods in TestClass. This will also exclude all calls below excluded method -->
     <pointcut name="ejbPointCutExclude"
        expression="(execution(* com.perf.test.TestClass.getName(..))) || (execution(* com.perf.test.TestClass.getCity(..))) || cflow(execution(* com.perf.test.TestClass.getCity(..))) || cflow(execution(* com.perf.test.TestClass.getName(..)))" />
    </concrete-aspect>              
</aspects>
</aspectj>

在抽象方面下创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public abstract  aspect EJBAspect
{

    public pointcut ejbPointCutInclude();
    public pointcut ejbPointCutExclude(): ;    


    before() : ejbPointCutInclude() &&  !ejbPointCutExclude()
    {              
        doBefore(thisJoinPointStaticPart.getSignature());      
    }

    after() :  ejbPointCutInclude() && !ejbPointCutExclude()
    {              
        doAfter(thisJoinPointStaticPart.getSignature());
    }
}