在尝试获取Aspect中的请求对象时,我找到了两个解决方案.我想知道哪个更好的表现.这是详细信息.
我想为’@myAnnotation’注释的所有方法执行myAspectMethod.所以Spring在方法级找到@myAnnotation,myAspectMethod将在我使用请求对象执行业务逻辑的地方执行.为了得到请求我找到了两个解决方案
>在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
}
}
>通过在带注释的方法中发送请求对象作为参数,并通过接收的参数列表访问它
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 {
}
>在两种不同的请求对象使用方式之间,从性能角度看哪一种更好?这是一个重要的问题,我想知道答案.
>每种方法的其他优缺点是什么?
最佳答案
>我不确定第一种方法是否有效.即使您可以通过这种方式自动装配HttpServletRequest,您也必须使用方面请求范围.
>我认为最好的选择是使用RequestContextHolder:
>我认为最好的选择是使用RequestContextHolder:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。