您的位置:首页 > 编程语言 > Java开发

spring aop 3 Spring AOP中JoinPoint的表达式定义描述

2016-01-14 14:47 597 查看
在使用Spring AOP的过程中,经常需要使用到各种不同的JoinPoint的定义,Spring AOP遵循了AspectJ形式的JoinPoint的定义形式,但是Spring目前只支持部分的AspectJ形式的Joinpoint的定义,同时Spring AOP只支持方法级别的JoinPoint。以下是我在学习Spring开发者手册时总结的一些JoinPoint的定义,不完整,欢迎补充。

execution方法签名需要满足execution中描述的方法签名
within包或者类型满足within中描述的包或者类型的类的所有非私有方法
thisSpring AOP 的代理实例的类型满足this中的描述的类型
target业务实例对象(非代理实例)的类型满足target 中的描述的类型
args方法的参数满参数类型为args中描述的类型
@target类型拥有@target描述中给出的annotation
@args方法运行时传入的参数的实际类型拥有@args描述中给出的annotation
@within类型拥有@target描述中给出的annotation,其中@target和@within的区别在于@within要求的annotation的级别为CLASS,而@target为RUNTIME
@annotation方法拥有@annotation 描述中给出的annotation
bean(idOrNameOfBean) bean的名字或者为bean描述中的名字或者Id
在使用Spring AOP中用的最多的应该是execution 形式的定义了,以下为它的定义格式:

Java代码


execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)

带?号的即为可选的,别的必须给出.

以下给出一些JointPoint的例子:

匹配任意的公共方法

Java代码


execution(public * *(..))

匹配任意方法名已set开头的方法

Java代码


execution(* set*(..))

匹配任意AccountService接口中的方法

Java代码


execution(* com.xyz.service.AccountService.*(..))

匹配任意AccountService接口中的方法

Java代码


execution(* com.xyz.service.AccountService.*(..))

匹配任意service包中任意类型中的方法

Java代码


execution(* com.xyz.service.*.*(..))

匹配任意service包或者service的子包中任意类型中的方法

Java代码


execution(* com.xyz.service..*.*(..))

匹配任意service包中任意类型中的方法

Java代码


within(com.xyz.service.*)

匹配任意service包或者service的子包中任意类型中的方法

Java代码


within(com.xyz.service..*)

匹配任意实现了AccountService接口的Spring AOP代理对象的任意方法

Java代码


this(com.xyz.service.AccountService)

匹配任意实现了AccountService接口的实际业务对象的任意方法

Java代码


this(com.xyz.service.AccountService)

匹配任意一个接收一个参数,并且运行时参赛类型为Serializable的方法

Java代码


args(java.io.Serializable)

匹配拥有@Transactional annotation类型的任意方法

Java代码


@target(org.springframework.transaction.annotation.Transactional)

匹配拥有@Transactional annotation类型的任意方法

Java代码


@within(org.springframework.transaction.annotation.Transactional)

匹配拥有@Transactional annotation的方法

Java代码


@annotation(org.springframework.transaction.annotation.Transactional)

匹配任意一个接收一个参数,并且运行时参赛类型拥有@Classified annotation的方法

Java代码


@args(com.xyz.security.Classified)

匹配bean的Id或者名字为tradeService的任意方法

Java代码


bean(tradeService)

匹配bean的Id或者名字以Service结尾的任意方法

Java代码


bean(*Service)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: