关于基于Annotation的Advice

基于Annotation与基于XmlSchema的AOP实际上是对代理过程的一个简化。 须要引包:asm-X.jar,asm-commons-X.jar,aspectjweaver.jar。 基于Annotation能够有3中声明方式: 一、将Pointcut单独声明在一个类文件中。 Pointcut的声明形式以下: @Pointcut("execution(* com.YH.类型名.*(..))")//pointcut的定义 public void point(){}//pointcut的签名 若是pointcut不少时,能够将若干个上面的pointcut单独的放在一个特定的类文件中。当调用的时候,在Advice所在类文件中做以下声明: @Before("pointcut类完整名.签名()")//BeforeAdvice @AfterReturing(pointcut="pointcut类完整名.签名()",returning="retVal") @AfterThrowing(pointcut="pointcut类完整名.签名()",throwing="throwable") @Around("pointcut类完整名.签名()") 二、在Advice所在类中声明Pointcut。 若是一个Pointcut须要被多个Advice引用,那么能够在Advice类文件中提早声明一个特定的Pointcut如: @Pointcut("execution(* com.YH.类型名.*(..))")//pointcut的定义 public void point(){}//pointcut的签名 而后再在每一个Advice声明中直接引用其签名便可。 三、每一个Advice都直接引用Pointcut的描述;如 @Before("execution(* com.YH.类型名.*(..))")//BeforeAdvice 实时证实,第一种与第二种方法,总会报错,多是JDK版本与AspectJ版本不一致所致。为了保持较高可移植性,尽可能用第三种方法。
相关文章
相关标签/搜索