原始springAop切面execution翻译及分析

官方链接:https://docs.spring.io/spring/docs/4.3.16.RELEASE/spring-framework-reference/htmlsingle/#aop-pointcuts-examples  具体位置在11.2.3 Declaring a pointcut 的Examples处

可对照原文参考,翻译如下:

用户常用Spring AOP中的execution表示式,表达式的格式如下:

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

以上表达式中 ret-type-pattern、name-pattern、param-pattern时必填项,其他都是非必填项。ret-type-pattern为方法返回值配置,切面为方法的返回值与之匹配的方法,经常我们把ret-type-pattern配置为*,意味着匹配任何返回值类型。

name-pattern匹配的是方法名,配置*也代表着匹配所有方法,declaring-type-pattern通过 `.` 连接name-pattern方法名,

param-pattern方法参数表达式稍微有些复杂:()表示匹配一个无参的方法,(..)代表匹配任何方法,(*)代表匹配有一个参数的方法,参数可以是任何类型,(*,String)代表匹配有两个参数的方法,第一个可以是任何类型的参数,第二个必须是String类型的参数。

以下为官方的几个execution的几个例子进行总结:

  • the execution of any public method:   切面为所有的public方法
execution(public * *(..))      //public 为modifiers-pattern 方法的作用域 * 为ret-type-pattern * 为name-pattern (..) 为param-pattern
  • the execution of any method with a name beginning with "set":
execution(* set*(..))  //* 为ret-type-pattern set* 为name-pattern (..) 为param-pattern
  • the execution of any method defined by the AccountService interface:
  • 为了快速读懂execution的配置信息,可以将里面的配置信息的三个必填项先分隔出来,再读其他内容的含义
execution(* com.xyz.service.AccountService.*(..))

先按照以上图片切割出必填的三项,根据翻译出来的内容,大家应该能很快知道其中含义,那么只剩下分析:

com.xyz.service.AccountService. 的含义是很么 ,当然根据上文的翻译,切面为定义在接口类AccountService中,

对应表达式中declaring-type-pattern 切面的范围配置。

在信息全部组合在一起就能直观的理解为:切面为com.xyz.service.AccountService中所有方法不限制任何参数不限制任何返回值,简洁点就是com.xyz.service.AccountService中的所有方法。

  • the execution of any method defined in the service package:
execution(* com.xyz.service.*.*(..))

分析切割剩下的 com.xyz.service.*.  范围为com.xyz.service包中,依据在英文描述中。

  • the execution of any method defined in the service package or a sub-package:
execution(* com.xyz.service..*.*(..))

分析切割剩下的 com.xyz.service..*.* 范围为com.xyz.service包及其子包中,依据在英文描述中。

猜你喜欢

转载自blog.csdn.net/fu250/article/details/80219415
今日推荐