Spring AOP point cut for 'nested' annotation

前端 未结 2 1798
独厮守ぢ
独厮守ぢ 2021-01-14 14:46

I need to define a point cut which triggers the execution on all methods of a spring service annotated with a custom annotation. The annotation I would like to define the po

2条回答
  •  花落未央
    2021-01-14 15:18

    I had this exact need in an application. I found this answer, but wasn't satisfied this couldn't be done.

    After a bit more searching, I found this cheat sheet for AspectJ/Spring pointcut expressions. The solution in the cheat sheet didn't work exactly as advertised, but I was able to make it work for what I needed.

    @Pointcut("within(@(@Annotation *) *)")
    public void classAnnotatedWithNestedAnnotationOneLevelDeep() { }
    

    I combined this expression with a @within expression for just the @Annotation to get what I wanted to work.

    For method execution:

    @Pointcut("execution(@(@com.someorg.SomeAnnotation *) * *(..))")
    public void methodAnnotatedWithNestedAnnotationOneLevelDeep() { }
    

    I combined this expression with a @annotation expression for just the @Annotation to get what I wanted to work for methods.

提交回复
热议问题