在Aspect中访问HttpServletRequest对象.哪一个是提到的两个解决方案之间的更好的解

Sid*_*ant 8 java performance aop spring

在尝试获取Aspect中的请求对象时,我找到了两个解决方案.我想知道哪个更好的表现.这是详细信息.

我想为'@myAnnotation'注释的所有方法执行myAspectMethod.所以Spring在方法级找到@myAnnotation,myAspectMethod将在我使用请求对象执行业务逻辑的地方执行.为了得到请求我找到了两个解决方案

  1. 在Aspect类中注入请求对象,
    如下所示

    @Aspect 
    public class MyAspect {
    @Autowired(required = true)
    **private HttpServletRequest request;**
    @Around("@annotation(myAnnotation)")
    public Object myAspectMethod(ProceedingJoinPoint pjp,  
            MyAnnotation myAnnotation) throws Throwable {
            //....do something with request object
            }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过在带注释的方法中发送请求对象作为参数,并通过接收的参数列表访问它

Aspect中的访问请求

@RequestMapping(method = { RequestMethod.GET }, value = "/something")
@MyAnnotation
public Object myAnnotatedMethod(**HttpServletRequest request**)
{
//....some business logic
}

@Aspect
public class MyAspect {
@Around("@annotation(myAnnotation)")
    public Object myAspectMethod(ProceedingJoinPoint pjp,
            MyAnnotation myAnnotation) throws Throwable {
            HttpServletRequest request = getRequestArgument(pjp);
            ....do something with request object
            }
    private HttpServletRequest getRequestArgument(ProceedingJoinPoint pjp) {
        for (Object object : pjp.getArgs()) {
            if (object instanceof HttpServletRequest) {
                return (HttpServletRequest) object;
            }
        }
        return null;
    }
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
}
Run Code Online (Sandbox Code Playgroud)
  1. 在两种不同的请求对象使用方式之间,从性能角度看哪一种更好?这是一个重要的问题,我想知道答案.

  2. 每种方法的其他优缺点是什么?

axt*_*avt 14

  1. 我不确定第一种方法是否有效.即使你可以通过HttpServletRequest这种方式自动装配,你也必须使用方面请求范围.

  2. 我认为最好的选择是使用RequestContextHolder:

    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    
    Run Code Online (Sandbox Code Playgroud)

    此方法使用已由Spring填充的线程本地存储,并且不需要对方法签名进行任何更改.