美文网首页
spring@Aspect注解拦截使用

spring@Aspect注解拦截使用

作者: 小民自愚 | 来源:发表于2021-03-18 14:57 被阅读0次

1.开启拦截
spring在xml中配置

<aop:aspectj-autoproxy />
2.定义注解

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Order(1)
public @interface PrintLog {
    boolean onlyErrorPrint() default false;
    String interfaceName();
}

3.定义拦截类

@Aspect
@Component
public class PrintLogScrutineer {
    private static Logger logger = LoggerFactory.getLogger(PrintLogScrutineer.class);

    /**
     * 打印日志
     */
    @Around("printLog()")
    public Object doLogRecord(ProceedingJoinPoint point) throws Throwable {
        Signature signature = point.getSignature();
        Object[] args = point.getArgs();
        String methodName = point.getSignature().getName();
        Class<?> classTarget = point.getTarget().getClass();
        Class<?>[] par = ((MethodSignature) point.getSignature()).getParameterTypes();
        Method objMethod = classTarget.getMethod(methodName, par);
        PrintLog aCache = objMethod.getAnnotation(PrintLog.class);
        String inputInfo = "【接口名:" + aCache.interfaceName() + "   " + signature.getDeclaringTypeName() + "." + signature.getName() + "】 入参 ---> " + JSON.toJSONString(args);
        String resultInfo = "";
        String errorInfo = "";
        boolean isError = false;
        try {
            Object result = point.proceed();
            resultInfo = "【接口名:" + aCache.interfaceName() + "   " + signature.getDeclaringTypeName() + "." + signature.getName() + "】 返回结果 ---> " + JSON.toJSONString(result);
            return result;
        } catch (NCZMemberCenterException providersMgException) {
            errorInfo = "【接口名:" + aCache.interfaceName() + "   " + signature.getDeclaringTypeName() + "." + signature.getName() + "】 异常 ---> " + providersMgException.toString();
            if (aCache.onlyErrorPrint()) {
                logInfoError(inputInfo);
                logInfoError(resultInfo);
                logInfoError(errorInfo);
                isError = true;
            }
            logInfoErrorStack(errorInfo, providersMgException);
            return Results.newFailedResult(providersMgException.getStateCode(), providersMgException.getMessage(), providersMgException.getMessage());
        } catch (Throwable throwable) {
            errorInfo = "【接口名:" + aCache.interfaceName() + "   " + signature.getDeclaringTypeName() + "." + signature.getName() + "】 异常 ---> " + throwable.toString();
            if (aCache.onlyErrorPrint()) {
                logInfoError(inputInfo);
                logInfoError(resultInfo);
                logInfoError(errorInfo);
                isError = true;
            }
            logInfoErrorStack(errorInfo, throwable);
//            throw throwable;
            return Results.newFailedResult(CommonStateCode.FAILED, throwable.getMessage(), throwable.getMessage());
        } finally {
            if (!aCache.onlyErrorPrint()) {
                if (isError) {
                    logInfoError(inputInfo);
                    logInfoError(resultInfo);
                    logInfoError(errorInfo);
                }
                logInfo(inputInfo);
                logInfo(resultInfo);
                logInfo(errorInfo);
            }
        }
    }

    private void logInfo(String info) {
        logger.info(info);
    }

    private void logInfoError(String info) {
        logger.error(info);
    }

    private void logInfoErrorStack(String info, Throwable e) {
        logger.error(info, e);
    }

    //定义切入点
    @Pointcut("@annotation(com.test.biz.annotation.PrintLog)")
    private void printLog() {
    }
}

4.使用

@PrintLog(interfaceName = "根据手机号获取UUID")
public Long test(String username, Integer customerType) {
        return 23456L;
    }

相关文章

网友评论

      本文标题:spring@Aspect注解拦截使用

      本文链接:https://www.haomeiwen.com/subject/tjaucltx.html