简体   繁体   中英

AspectJ Opening multiple threads

I am trying to use AspectJ for authorization and for request response logging. But the issue is it's opening two threads. Executing my controller and service method twice. Any help would be appriciated.

@Around("execution(* com.a.b.c.controller.*.*(..)) && @annotation(com.a.b.c.role.auth.ReadAuthorization) && args(request,obj)")
public Object before(ProceedingJoinPoint joinPoint,HttpServletRequest request,Object obj)      throws Throwable {
    Object result = null;
    if (!(request instanceof HttpServletRequest)) {
        throw new RuntimeException("You are not authorized");
    }
    
    LoggingObject loggingObject = new LoggingObject();
    loggingObject.setMethodName(MethodSignature.class.cast(joinPoint.getSignature()).getMethod().getName());
    loggingObject.setRequestObject(obj);
    try{
        Object requestObject = joinPoint.proceed();
        loggingObject.setResponseObject(requestObject);
        log.info(mapperObj.writeValueAsString(loggingObject));
    }catch(Exception e){
        loggingObject.setResponseObject(e);
        log.info(mapperObj.writeValueAsString(loggingObject));
    }

    if (auth.authorize(request.getHeader("id"),request.getHeader("token"))) {
        result = joinPoint.proceed();
        return result;
    } else {
        throw new RuntimeException("You are not authorized");
        
    }

}

I am risking an educated guess, even though the question is unclear with you talking about threads (see my questions in the comment about how you can improve the question):

What you probably mean is simply that the target method is executed twice, not that the aspect creates two threads, correct? Well, your advice method contains two proceed() calls. Why would you be surprised if the target method is executed twice if you call it twice by yourself?

I think that should answer your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM