这个执行切入点表达式是如何工作的?

How does this execution pointcut expression work?

我从@kriegaex 看到了一个answer,我无法理解。

我试图理解的切入点表达式如下

@Around("execution(* (@MyAnnotation *).*(..)) || execution(@MyAnnotation * *(..))")

据我了解,此表达式将建议任何 class 或用 @MyAnnotation

注释的方法

reference documentation开始,execution表达式的格式如下:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern)
            throws-pattern?)

基于此格式,表达式如下

execution(* (@MyAnnotation *).*(..))

可以解释为

ret-type-pattern is * 
declaring-type-pattern is any type with @MyAnnotation
name-pattern is *
param-pattern is ..

建议 class 中用 @MyAnnotation 注释的任何方法调用。如有错误请指正。

对于这个表达式

execution(@MyAnnotation * *(..))

我无法理解 modifiers-pattern 怎么会是 @MyAnnotation?这是如何工作的?

这是一个有趣的问题。有人在看文档,哇! :)

文档维护是 AspectJ 中的一个问题,因为现在的开发主要是由 Andrew (Andy) Clement 执行的单人秀。他主要忙于跟上 Java 发布周期和新语言功能,例如已经有开发者版本支持 Java 14(记录 classes)。

Spring 网站上的语法描述取自它也指向的 AspectJ Programming Guide。虽然定义仍然有效,但编程指南是在 Java 5 之前的时代编写的,即很久以前,IMO 从那时起就没有更新过。您可能会注意到它甚至根本没有提到注释。

与注释相关的所有内容都写在所谓的 The AspectJ 5 Development Kit Developer's Notebook 中,您会发现一小段提到:

Every join point has a single set of modifiers - these include the standard Java modifiers such as public, private, static, abstract etc., any annotations, and the throws clauses of methods and constructors. These modifiers are the modifiers of the subject of the join point.

因此对于 AspectJ,方法或 class 注释是其修饰符组的一部分,正如您已经正确指出的那样。


更新:您可能想考虑投资 Ramnivas Laddad 的书 "AspectJ in Action"。它也很旧(2009 年的第 2 版),但以扎实的方式涵盖了 AspectJ 和 Spring AOP 的基础知识。这是截图,我希望 Ramnivas and/or 出版商不要因此起诉我。这是为了鼓励购买这本书:


更新二: 貌似现在整本书甚至可以read online for free. The chapter from the screenshot is here,比如