在 Aspect 中访问 HttpServletRequest 对象。提到的两种解决方案之间,哪一个是更好的解决方案
Access HttpServletRequest object inside Aspect. Which one is better solution between two solutions mentioned
在尝试获取 Aspect 中的请求对象时,我找到了两个解决方案。我想知道哪个性能更好。详情如下。
我想为“@myAnnotation”注释的所有方法执行 myAspectMethod。因此,只要 spring 在方法级别找到 @myAnnotation,myAspectMethod 就会在我使用请求对象执行业务逻辑的地方执行。为了获得请求,我找到了两个解决方案
在Aspect中注入请求对象class like
以下
@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
:
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
此方法使用已由 Spring 填充的线程本地存储,不需要对您的方法签名进行任何更改。
第一种方法无效。
@Autowired(required = true)
private HttpServletRequest request;
确实有请求的特定数据。
我目前正在使用以下方法从我的请求
中提取自定义 headers
HttpServletRequest request = ((ServletRequestAttributes)
RequestContextHolder.currentRequestAttributes()).getRequest();
request.getHeader("mycustom");
在尝试获取 Aspect 中的请求对象时,我找到了两个解决方案。我想知道哪个性能更好。详情如下。
我想为“@myAnnotation”注释的所有方法执行 myAspectMethod。因此,只要 spring 在方法级别找到 @myAnnotation,myAspectMethod 就会在我使用请求对象执行业务逻辑的地方执行。为了获得请求,我找到了两个解决方案
在Aspect中注入请求对象class like
以下@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
:HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
此方法使用已由 Spring 填充的线程本地存储,不需要对您的方法签名进行任何更改。
第一种方法无效。
@Autowired(required = true)
private HttpServletRequest request;
确实有请求的特定数据。
我目前正在使用以下方法从我的请求
中提取自定义 headersHttpServletRequest request = ((ServletRequestAttributes)
RequestContextHolder.currentRequestAttributes()).getRequest();
request.getHeader("mycustom");