Spring(5) -(14) pointcut 语法

2018-09-26  本文已影响23人  小白201808

AOP的规范本应该由SUM公司提出,但是被AOP联盟捷足先登.AOP联盟指定AOP规范,首先就要解决一个问题,怎么表示切入点,也就是在哪些方法上增强(where)

AspectJ 是一个面向切面的框架:

AspectJ切入点语法如下:(表示在哪些包下的哪些类的哪些方法做切入增强)

execution(modifiners-pattern?ret-type-pattern declaring-type-pattern? name-param(param-patterm)throws-pattern?)
?表示:该参数可以出现一次或零次
翻译成中文:
execution(<修饰符>?<返回类型> <声明类型>?<方法名>(<参数>)<异常>?)
举例:public static Class java.lang.Class.forName(String className) throws ClassNotFoundException

通配符

*

匹配任何部分,只能表示一个单词

..

可用于全限定名中和方法参数中,分别表示子包和0到N个参数

spring-core 文档中的例子

Some examples of common pointcut expressions are given below.
the execution of any public method:
execution(public * *(..))

the execution of any method with a name beginning with "set":
execution(* set*(..))

the execution of any method defined by the AccountService interface:
execution(* com.xyz.service.AccountService.*(..))//常用

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

the execution of any method defined in the service package or a sub-package:
execution(* com.xyz.service..*.*(..))//常用
  
上一篇下一篇

猜你喜欢

热点阅读