spring aop 异常记录

来源:互联网 发布:淘宝差评可以删除吗 编辑:程序博客网 时间:2024/05/16 00:40


  1. 注解? 修饰符? 返回值类型 类型声明?方法名(参数列表) 异常列表?  

 

  • 注解:可选,方法上持有的注解,如@Deprecated;--
  • 该注解表示方法上----@within使用在类上
  • 切入的类和被切入的类必须是被spring管理的(springIOC),如果是自己new 出来的,切入无效。

 

 /**   * 异常记录   * */     @Pointcut("execution(* com.ybt.service..*.*(..) ) || execution(* com.ybt.util..*.*(..)) && !@within(org.aspectj.lang.annotation.AfterThrowing)")     public void exceptionHandler (){}; @Pointcut("execution(* com.ybt.util..*.*(..))") public void exceptionHandler2(){}; @AfterThrowing(pointcut = "exceptionHandler()",throwing="ex") public void exceptionHandlerAfterExp(JoinPoint joinPoint,Throwable ex){     StackTraceElement[] trace = ex.getStackTrace();     String exclass = trace[0].getClassName(); String method = trace[0].getMethodName(); String exceptionContent = "";     for (StackTraceElement s : trace) {     exceptionContent += "\tat " + s + "\r\n";     }          String params = joinPoint.getSignature().getName() + "( ";     for(Object arg : joinPoint.getArgs()){     params += arg+", ";          }     if(params.indexOf(",")  > 0){     params = params.substring(0,params.length() -2) + " )";     }else{     params = "";     }          YbtException exp = new YbtException();     exp.setExClass(joinPoint.getTarget().getClass()+"-->"+exclass);     exp.setException(ex.toString());     exp.setMethed(joinPoint.getTarget().getClass()+"."+joinPoint.getSignature().getName()+"()-->"+exclass+"."+ method+"()");     exp.setExceptionContent(exceptionContent);     exp.setParams(params);     exp.setExTime(new Date());     exceptionService.saveException(exp);  }


切入的类和被切入的类必须是被spring管理的(springIOC),如果是自己new 出来的,切入无效。
0 0