@AfterThrowing(pointcut = "within(org.springframework.tests.sample.beans.ITestBean+)", throwing = "ex") public void handleIOException(IOException ex) { handled++; lastException = ex; }
private Advice asAdvice(Method method) { if (method.getAnnotations().length == 0) return null; Before beforeAnn = method.getAnnotation(Before.class); if (beforeAnn != null) return new AdviceImpl(method,beforeAnn.value(),AdviceKind.BEFORE); After afterAnn = method.getAnnotation(After.class); if (afterAnn != null) return new AdviceImpl(method,afterAnn.value(),AdviceKind.AFTER); AfterReturning afterReturningAnn = method.getAnnotation(AfterReturning.class); if (afterReturningAnn != null) { String pcExpr = afterReturningAnn.pointcut(); if (pcExpr.equals("")) pcExpr = afterReturningAnn.value(); return new AdviceImpl(method,pcExpr,AdviceKind.AFTER_RETURNING,afterReturningAnn.returning()); } AfterThrowing afterThrowingAnn = method.getAnnotation(AfterThrowing.class); if (afterThrowingAnn != null) { String pcExpr = afterThrowingAnn.pointcut(); if (pcExpr == null) pcExpr = afterThrowingAnn.value(); return new AdviceImpl(method,pcExpr,AdviceKind.AFTER_THROWING,afterThrowingAnn.throwing()); } Around aroundAnn = method.getAnnotation(Around.class); if (aroundAnn != null) return new AdviceImpl(method,aroundAnn.value(),AdviceKind.AROUND); return null; }
@AfterThrowing("execution(* echo(*))") public void failed() { ++failureCount; }
@AfterThrowing(pointcut = "pointcut()", throwing = "e") public void doAfterThrowing(final JoinPoint joinPoint, final Throwable e) { try { this.logEvent(joinPoint, "ERROR", ExceptionUtils.getStackTrace(e)); } catch (final Exception ex) { log.error("异常信息:{}", ex.getMessage()); } }
/** * 抛出异常后通知:在连接节点抛出异常退出时执行的通知 */ @AfterThrowing("logaa1()") public void afterThrowingAdvice() { logger.info("LoggerHandlerAop.afterThrowingAdvice...time:" + DateUtil.now()); }
/** * 注解方法发生异常时执行,由于使用了环绕,此方法不会被调用 * @date 2016年10月2日上午9:37:28 * @param joinPoint * @param exception */ @AfterThrowing(throwing = "exception", pointcut = "explainAspect()") public void errorAop(JoinPoint joinPoint, Throwable exception) { //log.warn("@AfterThrowing"); }
/** * 进入方法后出现运行异常时运行的方法 * @param joinPoint 获取方法 * @param exception 方法处理时捕获的异常 * @return 对应拦截方法的返回参数 */ @AfterThrowing(value = "targets()", throwing = "exception") public Object throwing(JoinPoint joinPoint, Exception exception); }
@AfterThrowing(pointcut = "pointCut()", throwing = "ex") public void around(Exception ex) { }
@AfterThrowing(value = "redisAspect()", throwing = "ex") public void afterThrowing(Throwable ex) { throw new RuntimeException(ex); } }
@AfterThrowing("webLog()") public void throwss(JoinPoint jp){ System.out.println("方法异常时执行....."); }
@AfterThrowing(pointcut="allLogEventMethods()", throwing="ex") public void logAfterThrow(Throwable ex) { System.out.println("AFTER_THR: " + ex); }
/** * 指定拦截器规则;也可直接使用within(@org.springframework.web.bind.annotation.RestController *) * 这样简单点 可以通用 * @param 异常对象 */ @AfterThrowing(pointcut="pointcut()",throwing="e") public void afterThrowable(Throwable e) { log.error("切面发生了异常:", e); //这里可以做个统一异常处理 //自定义一个异常 包装后排除 //throw new AopException("xxx); }
@AfterThrowing(pointcut = "execution(* com.googlecode.scheme2ddl.UserObjectProcessor.process(..)) &&" + "args(userObject)", throwing = "ex") public void skippedBySQLError(UserObject userObject, CannotGetDDLException ex) { listSkippedBySQLError.add(userObject); }
@AfterThrowing(throwing = "t", pointcut = "updateInitialCodePointcut()") public void updateInitialCodeAfterThrowing(Throwable t) { logger.warn("", t); } }
@AfterThrowing(value = "distributedLockAspect()", throwing = "ex") public void afterThrowing(Throwable ex) { throw new RuntimeException(ex); }
@AfterThrowing("performance()") public void demandRefund() { System.out.println("Boo! We want our money back!"); }
@AfterThrowing(pointcut = "pointCut()", throwing = "exception") public void afterThrowing(JoinPoint joinPoint, Exception exception) { System.err.println("后置异常通知:" + exception); }