java Spring AOP - 每个带有注释的方法的切入点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7817822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:33:11  来源:igfitidea点击:

Spring AOP - pointcut for every method with an annotation

javaspringaopspring-aoppointcut

提问by emesx

I am trying to define a pointcut, that would catch every method that is annotated with (i.e.) @CatchThis. This is my own annotation.

我正在尝试定义一个切入点,它将捕获每个用 (ie) 注释的方法@CatchThis。这是我自己的注释。

Moreover, I'd like to have access to the first argument of the method, which will be of Longtype. There may be other arguments too, but I don't care about them.

此外,我想访问该方法的第一个参数,该参数的Long类型。可能还有其他争论,但我不在乎。



EDIT

编辑

This is what I have right now. What I don't know is how to pass the first parameter of the method annotated with @CatchThis.

这就是我现在所拥有的。我不知道的是如何传递带有注释的方法的第一个参数@CatchThis

@Aspect 
public class MyAspect {
    @Pointcut(value = "execution(public * *(..))")
    public void anyPublicMethod() {
    }

    @Around("anyPublicMethod() && @annotation(catchThis)")
    public Object logAction(ProceedingJoinPoint pjp, CatchThis catchThis) throws Throwable {
        return pjp.proceed();
    }
}

回答by Sean Patrick Floyd

Something like this should do:

这样的事情应该做:

@Aspect
public class MyAspect{

    @Pointcut(value="execution(public * *(..))")
    public void anyPublicMethod() {
    }

    @Around("anyPublicMethod() && @annotation(catchThis) && args(.., Long ,..)")
    public Object logAction(
        ProceedingJoinPoint pjp, CatchThis catchThis, Long long)
        throws Throwable {

        return pjp.proceed();
    }
}