1. 程式人生 > >java spring 配置異常攔截器

java spring 配置異常攔截器

在spring 中配置異常切面

<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="controllerExceptionAspect" class="com.connxun.config.aspect.ControllerExceptionAspect"/>
package com.connxun.config.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing
; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Arrays; /** * controller層異常攔截記錄 * * @author gaoyf */ @Aspect public class ControllerExceptionAspect { private static Logger logger = LoggerFactory.getLogger
("controllerLog"); @AfterThrowing(value = "execution (* com.connxun.portal.*.*(..))", throwing = "e") public void loggingException(JoinPoint joinPoint, Exception e) { // 攔截的實體類 Object target = joinPoint.getTarget(); // 攔截的方法名稱 String methodName = joinPoint.getSignature().getName(); logger.error("實體類:"
+ target); logger.error("方法名:" + methodName); logger.error("異常類名:" + joinPoint.getSignature().getName().getClass()); // 得到被攔截方法引數,並列印 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { logger.error("拋異常攔截: 被攔截到的方法引數:" + i + " -- " + args[i]); } logger.error("異常資訊: " + e.getMessage()); } @AfterThrowing(value = "execution (* com.connxun.portal.*.*(..))", throwing = "e") public void loggingException2(JoinPoint joinPoint, Exception e) { // 攔截的實體類 Object target = joinPoint.getTarget(); // 攔截的方法名稱 String methodName = joinPoint.getSignature().getName(); logger.error("實體類:" + target); logger.error("方法名:" + methodName); logger.error("異常類名:" + joinPoint.getSignature().getName().getClass()); // 得到被攔截方法引數,並列印 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { logger.error("拋異常攔截: 被攔截到的方法引數:" + i + " -- " + args[i]); } logger.error("異常資訊: " + e.getMessage()); } @Around("execution (* com.connxun.portal.*.*(..))") public Object serviceExceptionIterceptor(ProceedingJoinPoint joinPoint) throws Throwable { Object result = null; try { logger.info("The method " + joinPoint.getSignature().getDeclaringTypeName() + " " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs())); result = joinPoint.proceed(); logger.info("The method " + joinPoint.getSignature().getDeclaringTypeName() + " " + joinPoint.getSignature().getName() + "() ends with " + result); return result; } catch (IllegalArgumentException iae) {// 捕獲引數異常 StringBuilder sb = new StringBuilder(); sb.append(joinPoint.getTarget().getClass().getName() + " : " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()"); iae.printStackTrace(); logger.error(sb.toString()); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return result; } @Around("execution (* com.portal.*.*(..))") public Object serviceExceptionIterceptor2(ProceedingJoinPoint joinPoint) throws Throwable { Object result = null; try { logger.info("The method " + joinPoint.getSignature().getDeclaringTypeName() + " " + joinPoint.getSignature().getName() + "() begins with " + Arrays.toString(joinPoint.getArgs())); result = joinPoint.proceed(); logger.info("The method " + joinPoint.getSignature().getDeclaringTypeName() + " " + joinPoint.getSignature().getName() + "() ends with " + result); return result; } catch (IllegalArgumentException iae) {// 捕獲引數異常 StringBuilder sb = new StringBuilder(); sb.append(joinPoint.getTarget().getClass().getName() + " : " + Arrays.toString(joinPoint.getArgs()) + " in " + joinPoint.getSignature().getName() + "()"); iae.printStackTrace(); logger.error(sb.toString()); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return result; } }