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

查看:33
本文介绍了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.

编辑

这就是我现在所拥有的.我不知道的是如何传递用@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天全站免登陆