定义
格式:@ 注解(value=“表达标签 ( 表达式格式)”)
如:@Pointcut (value=“execution(* com.cn.spring.aspectj.NotVeryUsefulAspectService.*(…))”)
表达式标签
execution():用于匹配方法执行的连接点
args(): 用于匹配当前执行的方法传入的参数为指定类型的执行方法
this(): 用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配;
target(): 用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;
within(): 用于匹配指定类型内的方法执行;
@args():于匹配当前执行的方法传入的参数持有指定注解的执行;
@target():用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;
@within():用于匹配所以持有指定注解类型内的方法;
@annotation:用于匹配当前执行方法持有指定注解的方法;
其中execution 是用的最多的,
execution
execution格式:
execution(
modifier-pattern?
ret-type-pattern
declaring-type-pattern?
name-pattern(param-pattern)
throws-pattern?
)
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
其中带 ?号的 modifiers-pattern?,declaring-type-pattern?,hrows-pattern?是可选项
ret-type-pattern,name-pattern, parameters-pattern是必选项;
modifier-pattern? 修饰符匹配,如public 表示匹配公有方法
ret-type-pattern 返回值匹配,* 表示任何返回值,全路径的类名等
declaring-type-pattern? 类路径匹配
name-pattern 方法名匹配,* 代表所有,set*,代表以set开头的所有方法
(param-pattern) 参数匹配,指定方法参数(声明的类型),
(…)代表所有参数,
()代表一个参数,
(,String)代表第一个参数为任何值,第二个为String类型.
throws-pattern? 异常类型匹配
例子:
execution(public * (…)) 定义任意公共方法的执行
execution( set*(…)) 定义任何一个以"set"开始的方法的执行
execution(* com.xyz.service.AccountService.(…)) 定义AccountService 接口的任意方法的执行
execution( com.xyz.service..(…)) 定义在service包里的任意方法的执行
execution(* com.xyz.service ….(…)) 定义在service包和所有子包里的任意类的任意方法的执行
execution(* com.test.spring.aop.pointcutexp…JoinPointObjP2.*(…)) 定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
说明:最靠近(…)的为方法名,靠近.∗(…))的为类名或者接口名,如上例的JoinPointObjP2.∗(…))