spring aop unity catch exceptions

Aspect:

@Controller
@Aspect
public class WebExceptionAspect {

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")//连接点是@RequestMapping注解的方法
    private void webPointcut() {}

    @AfterThrowing(pointcut = "webPointcut()", throwing = "e")//切点在webpointCut()
    public void handleThrowing(JoinPoint joinPoint, Exception e) {//controller类抛出的异常在这边捕获
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        //开始打log
        System.out.println("异常:" + e.getMessage());
        System.out.println("异常所在类:" + className);
        System.out.println("异常所在方法:" + methodName);
        System.out.println("异常中的参数:");
        System.out.println(methodName);
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i].toString());
        }
    }

    @Before("execution(* com.xinjianqiao.mian.controller.*.*(..))")
    public void beforeProcess(JoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        //在项目中最好记录当前操作的时间和用户
        System.out.println("操作所在类:" + className);
        System.out.println("操作所在方法:" + methodName);
        System.out.println("操作中的参数:");
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i].toString());
        }
    }

    @AfterReturning(value = "execution(* com.xinjianqiao.mian.controller.*.*(..)))", returning = "returnVal")
    public void returnProcess(JoinPoint joinPoint, Object returnVal) {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        Class targetClass = null;
        String operationName = "";
        try {
            targetClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Method[] methods = targetClass.getMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs != null && clazzs.length == args.length&&
                        method.getAnnotation(ArchivesLog.class)!=null) {//这块是取出我们注解ArchiveLog中的值,一遍在日志时明确这个操作的名称
                    operationName = method.getAnnotation(ArchivesLog.class).operationName();
                    break;
                }
            }
        }
        System.out.println("操作名称:" + operationName);
        System.out.println("方法正常返回的值:" + returnVal);
    }
}

ArchivesLog:

@Target({ElementType.PARAMETER, ElementType.METHOD})  //注解可以用于参数或者方法上
@Retention(RetentionPolicy.RUNTIME)  //保留至运行时
@Documented//被javadoc所记录
public @interface ArchivesLog {
    /**
     * 操作类型
     * @return
     */
    public String operationType() default "";

    /**
     * 操作名称
     * @return
     */
    public String operationName() default "";

}

Controller:

@Controller
@RequestMapping("/exception")
public class ExceptionController {

    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8" )
    @ResponseBody
    @ArchivesLog(operationType = "测试", operationName = "测试异常或者测试返回")
    public JSONObject test(@PathVariable Integer id) throws Exception {
        JSONObject result = new JSONObject();
        try {//去掉注释可以测捕获的异常,不去掉注释可以测日志处理
            int i = 1;
             i=i/0;
        } catch (Exception ex) {
            throw new Exception("controller 层 异常");
        }
        return result;
    }
}

 

Guess you like

Origin blog.csdn.net/qq_42239765/article/details/91490611