700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 点点滴滴积累——基于XMLSchema与Annotation的几种Advice的规则

点点滴滴积累——基于XMLSchema与Annotation的几种Advice的规则

时间:2020-04-18 10:23:38

相关推荐

点点滴滴积累——基于XMLSchema与Annotation的几种Advice的规则

为什么80%的码农都做不了架构师?>>>

1.关于before advice:

对于before advice,是最简单的advice。无论是基于XmlSchema或者是基于Annotation,其advice对应的方法的参数只有一个,即JoinPoint类型的参数。在XmlSchema中其<aop:before.../>标签中的属性为①pointcut-ref或者piointcut②method

在Annotation中,也仅有一个属性,即pointcut,且这个属性可以被省略掉。如:@Before("execution(...)")

2.关于 after-returning advice:

after-returning advice就其对应的方法而言,需要分情况讨论。当基于XmlSchema来代理时,其对应方法的参数仅有一个,即JoinPoint类型的参数。而当基于Annotation的时候,其对应方法有两个参数,一个是JoinPoint型参数,另外一个是Object类型的参数(代表返回值)。

在Xml中,其标签对应的是<aop:after-returning.../>其属性有①pointcut-ref或者pointcut②method

在Annotation中,有两个属性:pointcut与returning。如:

@AfterRteturning(pointcut="execution(...)",returning="retVal")这个retVal就是Annotation情况下Advice对应方法中代表返回值的那个Object类型参数。

3.关于after-throwing advice:

after-throwing advice就对应方法来说,不论是基于XML还是基于Annotation都是具有两个参数:JoinPoint类型参数,与代表返回异常的Throwable类型参数。在xml中,对应标签为<aop:after-returning....>标签属性除了pintcut-ref(或者pintcut)与method外,还有一个属性throwing,其值一般为throwable。

基于Annotation时,带有两个属性:pointcut与throwing。如:@AfterThrowing(pointcut="execution(...)",throwing="throwable")这个throwable就是对应Annotation里面方法中的代表抛出异常的那个参数。

这里的异常类型需要在目标对象的接口中预先定义,异常类型可以是任何异常类型。

也就是说after returning advice在基于XmlSchema的时候对应方法仅一个参数,而throw advice无论在xmlSchema或者是基于Annotation都是有两个参数。而且,第二个参数总是要在xml或者Annotation中进行显示声明。即throwing=throwable

4.关于around advice

around advice是对before advice 与afterreturning advice两者都需要插入对象时的简化。类似before advice ,比较简单,这里不在赘述

另外,使用Annotation,需要引入aspectjweaver.jar

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。