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. PAT Basic 1004

    1004 成绩排名 (20 分) 读入 n(>0)名学生的姓名.学号.成绩,分别输出成绩最高和成绩最低学生的姓名和学号. 输入格式: 每个测试输入包含 1 个测试用例,格式为 第 1 行:正整数 ...

  2. Selenium Java环境配置

    Selenium Java环境配置 上次配置的是C#的环境,今天主要来配置一下Java环境. 首先,对于java环境配置最基础的JDK和JRE 先前我做过配置,这里就不重述了,网上的教程超级多.在基础 ...

  3. Django REST framework 中的序列化器

    在此之前定义一个序列化工具:     views中的的代码 from rest_framework.viewsets import ModelViewSet from .models import B ...

  4. python gui 之 tkinter库

    http://blog.csdn.net/jcodeer?viewmode=contents http://tieba.baidu.com/p/3082739560 http://blog.sina. ...

  5. es6冲刺01

    1.let/const 1)作用域:es5中有全局作用域.函数作用域.es6中新增了块级作用域 2)let定义的变量在所在块级作用域外失效,严格模式下失效后直接报错, 且不允许重复声明同名变量 3)c ...

  6. Keras运行速度越来越慢的问题

    Keras运行迭代一定代数以后,速度越来越慢,经检查是因为在循环迭代过程中增加了新的计算节点,导致计算节点越来越多,内存被占用完,速度变慢.判断是否在循环迭代过程中增加了新的计算节点,可以用下面的语句 ...

  7. php urlencode vs java URLEncoder.encode

    结论:urlencode 先比URLEncoder.encode多编码 “ * ” 符号,其他都保持一致 php urlencode  phpversion()>=5.3 will compli ...

  8. MVC Json方法里的一个坑

    MVC Controller类下面有这样一个方法 // // Summary: // Creates a System.Web.Mvc.JsonResult object that serialize ...

  9. APP微信支付Java后台的实现(springmvc)

    第一次做微信支付,阅读完开发文档后,下了个官方demo,摸索了好久,期间也出现了好多问题,终于是实现生成预支付订单以及支付成功后接收微信服务器通知,不多说了,直接上代码: 一.工具类 Constant ...

  10. Linux netstat常用命令

    1.统计80端口连接数netstat -nat|grep -i "80"|wc -l 2.统计httpd协议连接数(查看Apache的并发请求数及其TCP连接状态)ps -ef|g ...