aop异步获取数据异常

在AOP异步记录日志时 添加@Async注解后 无法获取不到http相关数据

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();一直空指针

去掉异步就可以获取到求解原因

@Async
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Aspect
@Component
public class AllContorllerLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(AllContorllerLogAspect.class);

    @Autowired
    private AspectMapper aspectMapper;

    Long startTime = null;
    Date beginDate = null;

    @Pointcut("execution(public * com.forms.pms2022.contorller.*.*(..))")
    public void pointcut() {
    }



    @Before("pointcut()")
    public void beforeRequest() {
        startTime = System.currentTimeMillis();
        beginDate = new Date();
    }

    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(value = "pointcut()", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {
        handleLog(joinPoint, null, jsonResult);
    }

    /**
     * 拦截异常操作
     *
     * @param joinPoint 切点
     * @param e         异常
     */
    @AfterThrowing(value = "pointcut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
        handleLog(joinPoint, e, null);
    }

    
    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) {
        logger.info("123456",RequestContextHolder.getRequestAttributes());
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        try {
            // 添加进数据库
            Operation operation = new Operation();
            operation.setStatus(StatusLog.SUCCESS.ordinal());
            // 请求的地址
            String ip = IpUtils.getIpAddr(request);
            operation.setOperIp(ip);
            operation.setOperUrl(request.getRequestURI());
            operation.setOperTime(beginDate);
            operation.setResponseTime((int) (System.currentTimeMillis() - startTime));

            if (e != null) {
                operation.setStatus(StatusLog.FAIL.ordinal());
            }
            // 设置方法名称
            String className = joinPoint.getTarget().getClass().getName();
            String methodName = joinPoint.getSignature().getName();
            operation.setMethod(className + "." + methodName + "()");
            // 设置请求方式
            operation.setRequestMethod(request.getMethod());
            // 保存数据库
            logger.info("参数:",operation);
            aspectMapper.aspect(operation);

        } catch (Exception exp) {
            // 记录本地异常日志
            logger.error("==前置通知异常==");
            logger.error("异常信息:{}", exp.getMessage());
            exp.printStackTrace();
        }
    }

    /**
     * 获取请求的参数,放到operation中
     * <p>
     * operation * @throws Exception 异常
     */
    private void setRequestValue(JoinPoint joinPoint, Operation operation) throws Exception {
        String requestMethod = operation.getRequestMethod();
        if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
            String params = argsArrayToString(joinPoint.getArgs());
            if (params.length() <= 2000 && params.length() >= 0) {
                operation.setOperParam(params);
            } else {
                operation.setOperParam(params.substring(0, 2000));
            }

        }
    }

    /**
     * 参数拼装
     */
    private String argsArrayToString(Object[] paramsArray) {
        String params = "";
        if (paramsArray != null && paramsArray.length > 0) {
            for (Object o : paramsArray) {
                if (!o.equals("") && o != "") {
                    try {
                        Object jsonObj = JSON.toJSON(o);
                        params += jsonObj.toString() + " ";
                    } catch (Exception e) {
                    }
                }
            }
        }
        return params.trim();
    }
}


在获取HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();一直空指针

我的解答思路和尝试过的方法
怎样才能获取到

异步放在service里,别放在controller。
1、在需要用到的@Async注解的类上加上@EnableAsync,或者直接加在springboot启动类上
2、异步处理方法(也就是加了@Async注解的方法)只能返回的是void或者Future类型
3、同一个类中调用异步方法需要先获取代理类,因为@Async注解是基于Spring AOP (面向切面编程)的,而AOP的实现是基于动态代理模式实现的

img