Spring AOP - pointcut for every method with an annotation

三世轮回 提交于 2019-12-31 08:29:15

问题


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

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

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

回答1:


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


来源:https://stackoverflow.com/questions/7817822/spring-aop-pointcut-for-every-method-with-an-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!