小编Sid*_*ant的帖子

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

在尝试获取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 …
Run Code Online (Sandbox Code Playgroud)

java performance aop spring

8
推荐指数
1
解决办法
5846
查看次数

标签 统计

aop ×1

java ×1

performance ×1

spring ×1