Spring AOP 抛出增强捕获异常

首先,需要两行配置

    <!--自定义切面所在包-->
    <context:component-scan base-package="cn.com.reformer.aop"/>
    <!--识别切面,开启CGLIB动态代理-->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

(一)自定义注解方法

        1.自定义注解       

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Log {
    LogType type() default LogType.SPACE;
}
      2.LogType枚举类型

public enum LogType {
    SPACE(""),
    INSERT("增加"),
    DELETE("删除"),
    UPDATE("修改"),
    QUERY("查询");

     private String description;
     private LogType( String string) {
         description=string;
    }

    public String GetDescription()
    {
        return description;
    }
}
      3.自定义切面

  //  @Pointcut("@annotation(cn.com.reformer.annotation.Log) && @annotation(log)")
  //  public  void serviceAspect(Log log) {
  //   }
   
 // @AfterThrowing(pointcut = "serviceAspect(log)", throwing = "e")
    @AfterThrowing(value = "@annotation(cn.com.reformer.annotation.Log) && @annotation(log)", throwing = "e")
    private void doAfterThrow(JoinPoint joinPoint, Log log, Throwable e){
        //读取中的用户
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Admin user = (Admin) request.getAttribute(Constants.CURRENT_ADMIN);
        ExceptionLog exceptionLog = new ExceptionLog();
        exceptionLog.setUserId(user.getId());
        exceptionLog.setIpAddr(getIP(request));
        exceptionLog.setOccurTime(new Date());
        Object[] os = joinPoint.getArgs();
        //获取类名
        String className = joinPoint.getTarget().getClass().getSimpleName();
        //获取方法名
        String methodName = joinPoint.getSignature().getName();
        String location = className + "." + methodName + ":";
        exceptionLog.setExceptionCode(location+e.getClass().getSimpleName()+e.getMessage());
        exceptionLog.setOperation(log.type().GetDescription());
        //写入数据库
        exceptionLogService.add(exceptionLog);
        return;
    }
    //获取请求IP
    public String getIP(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

(二)不用注解指定捕获异常的方法,确定需要捕获异常的包

controller目录任意下一级目录的任意类的所有方法

(..)表示任意参数

  @AfterThrowing(value = "execution(* cn.com.reformer.web.controller.*.*.*(..))", throwing = "e")
        private void doAfterThrow(JoinPoint joinPoint,  Throwable e) {
    }

其他同上


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值