java 使用注释从 Spring 2.5 中的 AOP 建议访问 HttpServletRequest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/977714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:39:36  来源:igfitidea点击:

Accessing HttpServletRequest from AOP advice in Spring 2.5 with annotations

javaspringannotationsaop

提问by Tomas

I have tried to find the answer to this question on both the Spring forum and by searching StackOverflow. I have found a lot of pages describing horrible architectures and asking for about the same thing as I do, but my intended usage is different so please bear with me :-)

我试图在 Spring 论坛和 StackOverflow 上找到这个问题的答案。我发现很多页面描述了可怕的架构,并要求与我做的事情大致相同,但我的预期用途不同,所以请耐心等待:-)

I have a Spring 2.5 project using annotation based form controllers basically like this:

我有一个使用基于注释的表单控制器的 Spring 2.5 项目,基本上是这样的:

@RequestMapping("/edit/someObject")
public String handleSubmit(HttpServletRequest request, HttpServletResponse response, SomeObject someObject, BindingResult result) {

    // Some check here

    if(result.hasErrors()) {
        return "form";
    } else {
        SomeObjectService.update(someObject);
        return "redirect:/view/someObject";
    }
}

In this I check for some http property in the HttpServletRequest and use the HttpServletResponse to send a redirect if this property has a certain value. This check is done is a lot (but not all) of the form controllers in this application. What I would like to do is create a @CheckedSubmit annotation handled by some AOP advice to do this check and then drop the HttpServletRequest and HttpServletResponse parameters from the controller.

在此,我检查 HttpServletRequest 中的某些 http 属性,如果该属性具有特定值,则使用 HttpServletResponse 发送重定向。这个检查是在这个应用程序中完成的很多(但不是全部)表单控制器。我想要做的是创建一个由一些 AOP 建议处理的 @CheckedSubmit 注释来执行此检查,然后从控制器中删除 HttpServletRequest 和 HttpServletResponse 参数。

My problem is that I have no idea how to access the current HttpServletRequest and HttpServletResponse from this AOP advice without using these two as (unused) parameters to the annotated method, which is what I tried to avoid in the first place.

我的问题是我不知道如何从这个 AOP 建议访问当前的 HttpServletRequest 和 HttpServletResponse 而不使用这两个作为注释方法的(未使用的)参数,这是我首先试图避免的。

Summary: How to access the HttpServletRequest/Response from AOP advice on an @RequestMapping annotated method?

摘要:如何从@RequestMapping 注释方法上的 AOP 建议访问 HttpServletRequest/Response?

回答by Gerrit Brehmer

Two solutions for the summary but not for your entire problem (completely)

总结的两种解决方案,但不适用于您的整个问题(完全)

Solution 1: inside advice (>= Spring 2.0 required)

解决方案 1:内部通知(需要>= Spring 2.0)

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
        .getRequestAttributes()).getRequest();

Solution 2: inside aspect class (probably Spring 3.0 inside singelton beans required!)

解决方案 2:在方面类内部(可能需要在单个 bean 中使用 Spring 3.0!)

@Autowired(required=true)
private HttpServletRequest request;

回答by skaffman

I think you probably know this already, but the "official" was to do this in Spring MVS is to use HandlerInterceptors. They're not annotation-driven, but they do get inserted into the HTTP control flow, and get full access to the request and response.

我想您可能已经知道这一点,但是在 Spring MVS 中“官方”这样做是使用 HandlerInterceptors。它们不是注解驱动的,但它们确实被插入到 HTTP 控制流中,并获得对请求和响应的完全访问权限。