切点

1 编写切点

@Pointcut 定义可重复使用的切点

@Pointcut("execution(public * com.xxx.mapper.*.*(..))")
public void pointCut() {
}

1.1 表示式

由下列方式来定义或者通过 &&、 ||、 !、 的方式进行组合:

1)方法
  • execution:匹配方法
  • within:限制匹配范围
  • this:匹配 AOP 代理类的执行方法
  • target:匹配目标类的执行方法
  • args:将匹配方法的参数传入通知,名称必须相同
2)注解
  • @within:匹配类注解
  • @target:匹配类注解
  • @args:匹配参数注解
  • @annotation:匹配方法注解
3)bean
  • bean():匹配制定 bean

1.2 格式

“?”代表可选项

execution(修饰符? 返回值 类路径? 方法名(参数)异常类型?) 
  • 返回值
    *匹配任意
  • 方法名
    *匹配任意
  • 参数
    指定参数类型,,隔开
    *任意类型
    ..任意个数

1.3 例子

  • 任意公共方法:execution(public * *(..))
  • 任何“set”开头方法:execution(* set*(..))
  • 某类任意方法:execution(* com.xyz.service.AccountService.*(..))
  • 某包里任意方法: execution(* com.xyz.service.*.*(..))
  • 某包及子包任意方法:execution(* com.xyz.service..*.*(..))
  • 某包任意类: within(com.test.spring.aop.pointcutexp.*)
  • 实现了 Intf 接口的所有类:this(com.abc.Intf)
  • 带有 @xxx 注解的所有类的任意方法:
    • @within(org.abc.xxx)
    • @target(org.abc.xxx))
  • 带有 @xxx 注解的任意方法:@annotation(org.abc.xxx))
  • 参数带有有 @xxx 注解的方法:@args(org.abc.xxx)
  • 参数类型: args(String)
Author: iMine
Link: https://imine141.github.io/2020/07/15/Spring/AOP/%E5%88%87%E7%82%B9/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.