只为成功找方向,不为失败找借口

每天都不能停止前进的脚步
随笔 - 370, 文章 - 0, 评论 - 211, 阅读 - 121万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

SpringMvc中获取Request

Posted on   冰碟  阅读(472)  评论(0编辑  收藏  举报

Controller中加参数

复制代码
复制代码
@Controller
public class TestController {
    @RequestMapping("/test")
    public void test(HttpServletRequest request) {
        ......
    }
}
复制代码
复制代码

Controller中获取request对象后,如果要在其他方法中(如service方法、工具类方法等)使用request对象,需要在调用这些方法时将request对象作为参数传入

此时request对象是方法参数,相当于局部变量,毫无疑问是线程安全的。 

自动注入

复制代码
复制代码
@Controller
public class TestController{
     
    @Autowired
    private HttpServletRequest request; //自动注入request
     
    @RequestMapping("/test")
    public void test() throws InterruptedException{
        ......
    }
}
复制代码
复制代码

使用这种方式,当Bean(本例的TestController)初始化时,Spring并没有注入一个request对象,而是注入了一个代理(proxy);当Bean中需要使用request对象时,通过该代理获取request对象。request实际上是一个代理:代理的实现参见AutowireUtils的内部类ObjectFactoryDelegatingInvocationHandler。

调用request的方法method时,实际上是调用了由objectFactory.getObject()生成的对象的method方法;objectFactory.getObject()生成的对象才是真正的request对象。

objectFactory的类型为WebApplicationContextUtils的内部类RequestObjectFactory;而RequestObjectFactory要获得request对象需要先调用currentRequestAttributes()方法获得RequestAttributes对象,生成RequestAttributes对象的核心代码在类RequestContextHolder中,生成的RequestAttributes对象是线程局部变量(ThreadLocal),因此request对象也是线程局部变量;这就保证了request对象的线程安全性。

基类中自动注入

复制代码
复制代码
public class BaseController {
    @Autowired
    protected HttpServletRequest request;     
}
@Controller
public class TestController extends BaseController {
}
复制代码
复制代码

与方法2相比,避免了在不同的Controller中重复注入request;但是考虑到java只允许继承一个基类,所以如果Controller需要继承其他类时,该方法便不再好用。

手动调用

复制代码
复制代码
@Controller
public class TestController {
    @RequestMapping("/test")
    public void test() throws InterruptedException {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
            .......
    }
}
复制代码
复制代码

通过自动注入实现与通过手动方法调用实现原理差不多。因此本方法也是线程安全的。

优点:可以在非Bean中直接获取。缺点:如果使用的地方较多,代码非常繁琐;因此可以与其他方法配合使用。

@ModelAttribute方法

复制代码
复制代码
@Controller
public class TestController {
    private HttpServletRequest request; 此处线程不安全
    @ModelAttribute
    public void bindRequest(HttpServletRequest request) {
        this.request = request;  此处request线程安全
    }
    @RequestMapping("/test")
    public void test() throws InterruptedException {
        ......
    }
}
复制代码
复制代码

@ModelAttribute注解用在Controller中修饰方法时,其作用是Controller中的每个@RequestMapping方法执行前,该方法都会执行。bindRequest()的作用是在test()执行前为request对象赋值。虽然bindRequest()中的参数request本身是线程安全的,但由于TestController是单例的,request作为TestController的一个域,无法保证线程安全。

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2017-11-01 SpringMVC与Struts2区别与比较总结
2017-11-01 Spring和SpringMVC的区别
点击右上角即可分享
微信分享提示