4.8AOP-XML-最终通知
戴着假发的程序员出品 抖音ID:戴着假发的程序员 欢迎关注
[查看视频教程]
所谓最终通知就是在方法完全执行结束之后在执行的通知,最终通知和后置通知的区别在于,无论业务方法是否出现异常中断,最终通知都会执行,和finally代码块的效果一致。
案例:
我们继续让业务方法抛出异常中断程序:
/** * @author 戴着假发的程序员 * @company http://www.boxuewa.com * @description */ public class MessageBean { //输出信息的业务方法 public String printMessage(String msg){ System.out.println("MessageBean-printMessage:"+msg); String str = null; str.trim();//抛出空指针异常 return msg; } }
在Aspect类中添加一个最终通知处理方法:
1 /** 2 * @author 戴着假发的程序员 3 * @company http://www.boxuewa.com 4 * @description 5 */ 6 public class DkAspect { 7 8 /** 9 * 最终通知 10 */ 11 public void after(JoinPoint joinPoint){ 12 System.out.println("最终通知。。。。。"); 13 } 14 }
在aop:config中添加最终通知的配置:
1 <!-- AOP配置 --> 2 <aop:config> 3 <!-- 申明AspectBean,引用我们注册的dkAspect --> 4 <aop:aspect id="aspect" ref="dkAspcet"> 5 <!-- 声明一个切入点,命名为pointcut1 --> 6 <!-- xml中不能使用 && ,逻辑与要使用and,--> 7 <!-- 如果我们的before增强方法中传入了参数msg,我就要使用args(msg)限定切入点 --> 8 <aop:pointcut id="pointcut1" 9 expression="execution(* com.st.beans..*.*(..))"/> 10 <!-- 最终通知 --> 11 <aop:after method="after" pointcut-ref="pointcut1"/> 12 13 </aop:aspect> 14 </aop:config>
执行业务方法测试:
我是戴着假发的程序员,分享技术,分享经验,如果要转载,请注明:出自戴着假发的程序员