AspectJ 开启多个线程

huangapple go评论77阅读模式
英文:

AspectJ Opening multiple threads

问题

我正在尝试使用AspectJ进行授权和请求响应日志记录。但问题是它会开启两个线程。执行我的控制器和服务方法两次。任何帮助将不胜感激。

@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 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");
		
	}

}

答案1

得分: 1

我正在冒险做一个有根据的猜测,尽管你提到线程的问题不太清楚(请看我在评论中关于如何改进问题的问题):

你可能的意思只是目标方法被执行了两次,而不是切面创建了两个线程,是这样吗?嗯,你的建议方法包含了两个proceed()调用。如果你自己调用了它两次,那么目标方法被执行两次你为什么会感到惊讶呢?

我认为这应该能回答你的问题。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月20日 23:27:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63508368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定