对于那些在程序中直接抛给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>