Spring的aop开发(四)通知类型

引用文章https://www.cnblogs.com/liuruowang/p/5711563.htm

(1)Before:在目标方法被调用之前做增强处理,@Before只需要指定切入点表达式即可

<aop:before method="aspect" pointcut-ref="pointcut"></aop:before>

(2)AfterReturning:在目标方法正常完成后做增强,@AfterReturning除了指定切入点表达式后,还可以指定一个返回值形参名returning,代表目标方法的返回值

<aop:after-returning method="aspect" pointcut-ref="pointcut"></aop:after-returning>

(3)AfterThrowing:主要用来处理程序中未处理的异常,@AfterThrowing除了指定切入点表达式后,还可以指定一个throwing的返回值形参名,可以通过该形参名

来访问目标方法中所抛出的异常对象

<aop:after-throwing method="aspect"pointcut-ref="pointcut"></aop:after-throwing>

(4)After:在目标方法完成之后做增强,无论目标方法时候成功完成。@After可以指定一个切入点表达式

<aop:after method="aspect" pointcut-ref="pointcut"></aop:after>

(5)Around:环绕通知,在目标方法完成前后做增强处理,环绕通知是最重要的通知类型,像事务,日志等都是环绕通知,注意编程中核心是一个ProceedingJoinPoint    会阻止目标方法进行

做环绕通知的时候,调用ProceedingJoinPoint的proceed()方法才会执行目标方法。

<aop:around method="aspect" pointcut-ref="pointcut"></aop:around>

6.通知执行的优先级

进入目标方法时,先织入Around,再织入Before,退出目标方法时,先织入Around,再织入AfterReturning,最后才织入After。

注意:Spring AOP的环绕通知会影响到AfterThrowing通知的运行,不要同时使用!同时使用也没啥意义。

猜你喜欢

转载自blog.csdn.net/weixin_42369687/article/details/88991322