Spring AOP - 带有注释的每个方法的切入点 [英] Spring AOP - pointcut for every method with an annotation

查看:164
本文介绍了Spring AOP - 带有注释的每个方法的切入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

此外,我想访问该方法的第一个参数,该参数将是 Long 类型。可能还有其他论点,但我不关心它们。

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

EDIT

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

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.

@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();
    }
}


推荐答案

东西这样应该这样做:

@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();
    }
}

这篇关于Spring AOP - 带有注释的每个方法的切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆