spring aop pointcut语法

@PointCut表达式标签

execution():用于匹配方法执行的连接点

args(): 用于匹配当前执行的方法传入的参数为指定类型的执行方法

this(): 用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配;

target(): 用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配

within(): 用于匹配指定类型内的方法执行;

@args():于匹配当前执行的方法传入的参数持有指定注解的执行;

@target():用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;

@within():用于匹配所以持有指定注解类型内的方法;

@annotation:用于匹配当前执行方法持有指定注解的方法;


其中

execution使用的是最多的,使用举例

execution(public * *(..)) 定义任意公共方法的执行

execution(* set*(..)) 定义任何一个以"set"开始的方法的执行

execution(* com.sb.service.AccountService.*(..)) 定义AccountService 接口的任意方法的执行

execution(* com.sb.service.*.*(..)) 定义在service包里的任意方法的执行

execution(* com.sb.service ..*.*(..)) 定义在service包和所有子包里的任意类的任意方法的执行

execution(* com.sb.spring.aop.pointcutexp…JoinPointObjP2.*(…)) 定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:


AspectJ类型匹配的通配符

* 匹配任何数量字符;

匹配任何数量字符;

. . 匹配任何数量字符的重复,如在类型模式中匹配任何数量子包;而在方法参数模式中匹配任何数量参数。

匹配任何数量字符的重复,如在类型模式中匹配任何数量子包;而在方法参数模式中匹配任何数量参数。

+:匹配指定类型的子类型;仅能作为后缀放在类型模式后边。

匹配指定类型的子类型;仅能作为后缀放在类型模式后边。

java.lang.String 匹配String类型;

java.*.String 匹配java包下的任何“一级子包”下的String类型;如匹配java.lang.String,但不匹配java.lang.ss.String

java..* 匹配java包及任何子包下的任何类型; 如匹配java.lang.String、java.lang.annotation.Annotation

java.lang.*ing 匹配任何java.lang包下的以ing结尾的类型;

java.lang.Number+ 匹配java.lang包下的任何Number的自类型;如匹配java.lang.Integer,也匹配java.math.BigInteger

within和@within

within(com.test.spring.aop.pointcutexp.*) pointcutexp包里的任意类.

within(com.test.spring.aop.pointcutexp…*) pointcutexp包和所有子包里的任意类.

@within(org.springframework.transaction.annotation.Transactional) 带有@Transactional标注的所有类的任意方法.

@target

@target(org.springframework.transaction.annotation.Transactional) 带有@Transactional标注的所有类的任意方法,使用“@target(注解类型)”匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;注解类型也必须是全限定类型名;

@annotation @annotation(org.springframework.transaction.annotation.Transactional) 带有@Transactional标注的任意方法.使用“@annotation(注解类型)”匹配当前执行方法持有指定注解的方法;注解类型也必须是全限定类型名

示例描述@annotation(cn.javass.spring.chapter6.Secure )当前执行方法上持有注解 cn.javass.spring.chapter6.Secure将被匹配


@within和@target针对类的注解,@annotation是针对方法的注解


@within和@target针对类的注解,@annotation是针对方法的注解


args 和 @args

@args(org.springframework.transaction.annotation.Transactional) 参数带有@Transactional标注的方法.

args(String) 参数为String类型(运行是决定)的方法.


Pointcut定义时,还可以使用&&、∣∣、!运算符

比如:

spring aop pointcut语法


分享到:


相關文章: