如何指定包含层次结构中包含所有公共方法但不包括某些特定方法的切入点?

时间:2015-07-17 18:54:36

标签: aspectj

我有以下简单的方面。

package com.example.foo.aspects;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class TimingProfiler
{
    private static final Log logger = LogFactory.getLog(TimingProfiler.class);

    @Pointcut("execution(String *.toString())"
            + " || execution(int *.hashCode())"
            + " || execution(boolean *.equals(Object))"
            + " || execution(int *.getId())"
            + " || execution(String *.getName())")
    public void whatIDontWantToMatch(){}

    @Pointcut("execution(public * com.example.foo..*(..))")
    public void whatIWantToMatch(){}

    @Pointcut("whatIWantToMatch() && ! whatIDontWantToMatch()")
    public void allIWantToMatch(){}

    @Around("allIWantToMatch()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable
    {
        Signature signature = joinPoint.getStaticPart().getSignature();
        logger.info("Entering method " + signature);

        Object returnValue = joinPoint.proceed();

        logger.info("Leaving method " + signature);

        return returnValue;
    }
}

但是,我在编译期间收到以下消息。

[INFO] Join point 'method-execution(com.example.foo.bar.baz.snack.SnackIdentifier com.example.foo.bar.baz.snack.Snack.getId())' in Type 'com.example.foo.bar.baz.snack.Snack' (Snack.java:78) advised by around advice from 'com.example.foo.aspects.TimingProfiler' (foo-profiler-lib.jar!TimingProfiler.class(from TimingProfiler.java))

当我运行代码时,我会从我试图排除的方法中获取日志消息。

如何正确包含com.example.foo包中的所有公开方法,同时排除某些特定方法?

1 个答案:

答案 0 :(得分:0)

我忽略了我的PointCut排除了int getId()这一事实,我看到的输出方法是SnackIdentifier getId()。这个问题的正确答案是要注意。