Spring AOP pointcut 表达式

Wildcard

  • *: 匹配任意数量的字符
  • +:匹配制定数量的类及其子类
  • ..:一般用于匹配任意数量的子包或参数
    详细示例见后面的例子

Operators

  • &&:与操作符
  • ||:或操作符
  • !:非操作符

Designators

1. within()

//匹配productService类中的所有方法
@pointcut("within(com.sample.service.productService)")
public void matchType()

//匹配sample包及其子包下所有类的方法
@pointcut("within(com.sample..*)")
public void matchPackage()

2. 匹配对象(this, target, bean)

this(AType) means all join points where this instanceof AType is true. So this means that in your case once the call reaches any method of Service this instanceof Service will be true.

this(Atype)意思是连接所有this.instanceof(AType) == true的点,所以这意味着,this.instanceof(Service) 为真的Service实例中的,所有方法被调用时。

target(AType) means all join points where anObject instanceof AType . If you are calling a method on an object and that object is an instanceof Service, that will be a valid joinpoint.

target(AType)意思是连接所有anObject.instanceof(AType)。如果你调用一个Object中的一个方法,且这个object是Service的实例,则这是一个合法的切点。
To summarize a different way - this(AType) is from a receivers perspective, and target(AType) is from a callers perspective.

总结:this(AType)同接受者方面描述,target(AType)则从调用者方面描述。

  • this
@pointcut("this(com.sample.demoDao)")
public void thisDemo()
  • target
@pointcut("target(com.sample.demoDao)")
public void targetDemo()
  • bean
//匹配所有以Service结尾的bean里面的方法
@pointcut("bean(*Service)")
public void beanDemo

3. 参数匹配

  • bean
//匹配所有以find开头,且只有一个Long类型参数的方法
@pointcut("execution(* *..find*(Long))")
public void argDemo1()

//匹配所有以find开头,且第一个参数类型为Long的方法
@pointcut("execution(* *..find*(Long, ..))")
public void argDemo2()
  • arg
@pointcut("arg(Long)")
public void argDemo3()

@pointcut("arg(Long, ..)")
public void argDemo4()

4. 匹配注解

  • @annotation
//匹配注解有AdminOnly注解的方法
@pointcut("@annotation(com.sample.security.AdminOnly)")
public void demo1()
  • @within
//匹配标注有admin的类中的方法,要求RetentionPolicy级别为CLASS
@pointcut("@within(com.sample.annotation.admin)")
public void demo2()
  • @target
//注解标注有Repository的类中的方法,要求RetentionPolicy级别为RUNTIME
@pointcut("target(org.springframework.stereotype.Repository)")
public void demo3()
  • @args
//匹配传入参数的类标注有Repository注解的方法
@pointcut("args(org.springframework.stereotype.Repository)")
public void demo3()

5. execution()

格式:

execution(<修饰符模式>? <返回类型模式> <方法名模式>(<参数模式>) <异常模式>?)

标注❓的项目表示着可以省略

execution(
    modifier-pattern?  //修饰符
    ret-type-pattern  //返回类型
    declaring-type-pattern?  //方法模式
    name-pattern(param-pattern)  //参数模式
    throws-pattern?  //异常模式
)

/*
整个表达式可以分为五个部分:
 1、execution(): 表达式主体。
 2、第一个*号:表示返回类型,*号表示所有的类型。
 3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。
 4、第二个*号:表示类名,*号表示所有的类。
 5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。
*/
@pointcut("execution(* com.sample.service.impl..*.*(..))")
posted @ 2021-03-30 17:06  镇魂帆-张  阅读(131)  评论(0编辑  收藏  举报