AOP表达式中常用的指示符,如图(摘自慕课网):
AOP相关指示符说明

包、类型

  • within()
    表示匹配该包以及子包下面所有类的方法
	/**
     * within(com.xanthuim.springaop.service..*)
     */
    @Pointcut("within(com.xanthuim.springaop.service.impl.*)")
    public void pointcut() {

    }

类(对象)

  • this()
    匹配类、接口实现类中所有方法,包括未在接口中声明的方法
  • target()
    匹配类、接口实现类中所有方法,包括未在接口中声明的方法。在一般情况下与this()一致,二者的区别体现在通过引介切面产生代理对象时的具体表现。具体参考:https://blog.csdn.net/yangshangwei/article/details/77861658
  • bean()
    拦截Spring环境中Bean类的方法,也可以使用匹配符
	@Pointcut("bean(orderServiceImpl)")
    public void pointcut() {

    }

方法

  • execution()
    完整的表达式:execution(修饰符匹配符 返回值匹配符 包名匹配符 方法名匹配符(参数匹配符) 异常匹配符)
	// 匹配任何以find开头、任何参数的方法
    @Pointcut("execution(* *..find*(..))")
    public void pointcut() {

    }

参数

  • args()
	// 匹配第一个参数为Long类型,并且必须是com.xanthuim.springaop.service包下的任意类的方法
    @Pointcut("args(Long, ..) && within(com.xanthuim.springaop.service.*)")
    public void pointcut() {

    }

注解(注意注解有继承也会受影响)

  • @annotation()
	// 匹配方法上标注有@DataSource注解的方法
    @Pointcut("@annotation(com.xanthuim.springaop.annotation.DataSource)")
    public void pointcut() {

    }
  • @within()
	// 匹配类上标注有@DataSource注解的方法,并且要求注解的RetentionPolicy级别为CLASS
    @Pointcut("@within(com.xanthuim.springaop.annotation.DataSource)")
    public void pointcut() {

    }
  • @target()
	// 匹配类上标注有@DataSource注解的方法,并且要求注解的RetentionPolicy级别为RUNTIME
    @Pointcut("@within(com.xanthuim.springaop.annotation.DataSource)")
    public void pointcut() {

    }
  • @args()
	// 匹配参数上标注有@DataSource的方法。比如login(User user)方法中的User类被@DataSource标注了,那么这个方法就可以被拦截。
    @Pointcut("@within(com.xanthuim.springaop.annotation.DataSource)")
    public void pointcut() {

    }

相关文章: