在尝试获取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
}
}
Run Code Online (Sandbox Code Playgroud)通过在带注释的方法中发送请求对象作为参数,并通过接收的参数列表访问它
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)