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的一个域,无法保证线程安全。

SpringMvc中获取Request的更多相关文章

  1. springMVC中获取request和response对象的几种方式(RequestContextHolder)

    springMVC中获取request和response对象的几种方式 1.最简单方式:参数 2.加入监听器,然后在代码里面获取 原文链接:https://blog.csdn.net/weixin_4 ...

  2. 在SpringMVC中获取request对象

    1.注解法 @Autowired private  HttpServletRequest request; 2. 在web.xml中配置一个监听 <listener> <listen ...

  3. 在SpringMVC中获取request对象的几种方式

    1.最简单的方式(注解法) @Autowired private HttpServletRequest request; 2.最麻烦的方法 a. 在web.xml中配置一个监听 <listene ...

  4. 如何在SpringMVC中获取request对象

    1.注解法 @Autowired private HttpServletRequest request; <listener> <listener-class> org.spr ...

  5. springmvc中获取request对象,加载biz(service)的方法

    获取request对象: 首先配置web.xml文件--> <listener> <listener-class> org.springframework.web.con ...

  6. struts2中获取request、response,与android客户端进行交互(文件传递给客户端)

    用struts2作为服务器框架,与android客户端进行交互需要得到request.response对象. struts2中获取request.response有两种方法. 第一种:利用Servle ...

  7. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  8. Spring中获取request的几种方法,及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  9. 【转】SpringMVC,获取request的几种方法,及线程安全性

    作者丨编程迷思 https://www.cnblogs.com/kismetv/p/8757260.html 概述 在使用Spring MVC开发Web系统时,经常需要在处理请求时使用request对 ...

随机推荐

  1. [CSAcademy]Or Problem

    [CSAcademy]Or Problem 题目大意: 一个长度为\(n(n\le2\times10^5)\)的序列\(A(0\le A_i<2^{20})\),将其分为恰好\(m\)个连续段, ...

  2. BZOJ4962 : 简单的字符串

    枚举子串的中心,往两侧扩展,将两侧对应位置的字符交替写下来,得到一个字符串$S$. 若前后长度为$L$的子串循环同构,则在$S$中它们对应长度为$2L$的前缀,需要满足它可以由不超过$2$个偶回文串拼 ...

  3. python-校验密码小练习

    #校验密码是否合法的小练习#1.密码长度5到10位:#2.密码里面必须包含,大写字母,小写字母,数字#3.最多输入5次 写程序过程中遇到了两个问题,第二个循环里的P是把password的值循环传到p里 ...

  4. 获取SQL server 中的表和说明

    SELECT 表名 = case when a.colorder = 1 then d.name                    else '' end,        表说明 = case w ...

  5. Linux之nginx反向代理+三台web+nfs共享存储实现集群配置

    作业四:nginx反向代理+三台web+nfs共享存储实现集群配置 在各个web服务器上挂载nfs [root@localhost nginx]# mount -t nfs 192.168.152.1 ...

  6. Linux Kernel API

    记录一些Linux Device Drivers中常用的API. Linux官方提供的内核文档: 1. 最新版: https://www.kernel.org/doc/html/latest/ 2. ...

  7. Android SDK版本号 与 API Level 对应关系

    转自:https://blog.csdn.net/qiaoquan3/article/details/70185550 Android SDK版本号 与 API Level 对应关系   新接触And ...

  8. nginx代理后,获取request的ip

    应用程序部署上线,一般都会用nginx之类的来进行反向代理,而不是直接访问tomcat之类的容器. 这时候如果用平时的获取ip的代码,就只会获取到nginx所在服务器的ip, 就失去了本身的意义. 今 ...

  9. 表型数据(Phenotype Data)基本概念

    表型(英语:Phenotype),又称表现型,对于一个生物而言,表示它某一特定的物理外观或成分.一个人是否有耳珠.植物的高度.人的血型.蛾的颜色等等,都是表型的例子. 表型主要受生物的基因型和环境影响 ...

  10. centos修改主机名 root@后面的名字

    阿里云买的新的ESC,名字都是一串字符,不利于平时使用.我们可以重命名主机来标记. centos6 [root@centos6 ~]$ hostname # 查看当前的hostnmae centos6 ...