spring中对异常的统一处理

来源:互联网 发布:电力系统优化matlab 编辑:程序博客网 时间:2024/05/01 01:28

对于那些在程序中直接抛给spring的异常,spring该如何处理:
运用aop的思想,首先定义一个spring异常的处理类,如下:

/** * 异常处理类,把异常打印到日志中 * @author zyq * */public class ExceptionHandler implements ThrowsAdvice {    private static Logger LOGGER = Logger.getLogger(ExceptionHandler.class);        public void afterThrowing(Exception e) throws Throwable {            LOGGER.error("=============【发生error】================");            LOGGER.error("", e);         }}

2.在xml中配置相关aop:

<bean id="exceptionHandler" class="jt.comm.ExceptionHandler" />     <aop:config>         <aop:aspect ref="exceptionHandler">             <aop:pointcut id="exceptionService" expression="execution(* jt.controller.background.*.*(..))" />             <aop:after-throwing pointcut-ref="exceptionService" method="afterThrowing" throwing="e" />         </aop:aspect>     </aop:config>  
0 0
原创粉丝点击