springmvc httprequest 使用@Autowired注解我一直有个疑问,就是注解后每次的httprequest 是不是都一样的了,然后会不会引发多线程问题?

代码如下:

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestController{ @Autowired
HttpServletRequest request1;//这样使用,request1每次进来都是Current Request @RequestMapping("/test1")
public void test(){
System.out.println("test1request1:"+request1.hashCode());
System.out.println("test1"+request1.getParameter("name"));
} @RequestMapping("/test2")
public void test2(String name){
System.out.println("request2request2:"+request1.hashCode());
System.out.println("test2"+request1.getParameter("name"));
} @RequestMapping("/test3")
public void test3(HttpServletRequest request){
//这样进来request每次都不一样
System.out.println("request:"+request1.hashCode()); } }

springmvc常见注入HttpServletRequest 是使用方法级别注入的,它的基本思想是一个请求就是一个新实列HttpServletRequest,所以不会出现所谓多线程问题,毕竟是局部变量控制了。

而我们使用@Autowired来注入一个全局实例是否会出现多线程问题?跟踪源码,发现当前注入每次Current Request,然后这个request其实保存在一个ThreadLocal中的,这样其实它的底层实现方法就是针对每次来的request都保存在一个ThreadLocal中,然后去取,所以这样写代码是没有问题的。

总结一下两种写法都没有问题。但是用得最多的是第一种写法,毕竟我们还是要顺着springmvc的设计初衷考虑,还有从多线程设计思路及代码思路考虑,尽量不要用全局变量。因为这容易引入并发问题......

springmvc httprequest 使用@Autowired注解的更多相关文章

  1. 解决 SpringMVC 非spring管理的工具类使用@Autowired注解注入DAO为null的问题

    在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在Controller层中注入service接口,在service层中注入其它的s ...

  2. springmvc入门基础之注解和参数传递

    一.SpringMVC注解入门 1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- mvc的注解驱动 --> <mvc:annotation ...

  3. Spring@Autowired注解

    @Autowired注解可以对成员变量.方法和构造函数进行标注,来完成自动装配的工作. 注意:@Autowired默认是按照类型来注入的. 看下面的例子:例子是以对成员变量(field)为例进行的 p ...

  4. 常见的springmvc、SpringBoot的注解

    springMvc的常用注解 : @Controller :用于标记在一个类上,使用它标记的类就是一个springmcv Controller对象,分发处理器将会扫描使用了该注解 的类的方法,并检测该 ...

  5. 解决非controller使用@Autowired注解注入为null问题

    在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在controller层中注入service接口,在service层中注入其它的s ...

  6. SpringMVC的 几个注解

    1.@RequestMapping: 是一个用来处理请求地址映射的注解,可用于类或方法上. 1):用在类上:是父路径. 2):用在方法上:是子路径. @Controller //设置想要跳转的父路径 ...

  7. Spring5:@Autowired注解、@Resource注解和@Service注解

    什么是注解 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分 ...

  8. @Autowired注解的使用

    使用Spring时,通过Spring注入的Bean一般都被定义成private,并且要有getter和setter方法,显得比较繁琐,增加了代码量,而且有时会搞忘造成错误. 可以使用@Autowire ...

  9. 【Java EE 学习 83 下】【SpringMVC】【使用注解替代已过时的API】【SpringMVC、Hibernate整合】

    一.SpringMVC中注解的使用 1.为什么要使用注解 之前曾经提到过的三种控制器在spring3.0中都已经被明确标记为过时了,spring3.0推荐使用注解的方式替代三种控制器,实际上使用注解的 ...

随机推荐

  1. vim 编辑器IDE版

    wget https://raw.github.com/ma6174/vim/master/setup.sh -O ma6174_vim_setup.sh && bash ma6174 ...

  2. MySQL 数据底部出现总计字样 第二种办法 纵向合并 20161103

    上次在博客http://www.cnblogs.com/Mr-Cxy/p/5923375.html 我们使用了group by with rollup 函数 field自定义排序 来实现添加底部总计字 ...

  3. 【Lintcode】062.Search in Rotated Sorted Array

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  4. requirejs的打包工具r.js

    不建议用命令行,还是用配置文件比较方便--build.js. 我的build.js文件内容大概如下: ( { appDir : './', baseUrl : './scripts', dir : ' ...

  5. The specified named connection is either not found in the configuration, not intended to be used

    今天用EF遇到一个问题, The specified named connection is either not found in the configuration, not intended t ...

  6. idea自动重置language level和java compiler解决办法:修改setting

    maven工程: 错误: -source 1.6 中不支持 diamond 运算符. 尝试按网的的方式修改后,自动恢复,也在pom文件指定版本,依然不行. 后来发现:pom这样配置了: <plu ...

  7. AngularJs(Part 1)

    I am tired to translate these into Chinese. but who cares? i write these posts just for myself Scope ...

  8. Gym 101142C CodeCoder vs TopForces (搜索)

    题意:每个人有2种排名,对于A只要有一种排名高于B,那么A就能赢B,再如果B能赢C,那么A也能赢C,要求输出每个人分别能赢多少个人 析:首先把题意先读对了,然后我们可以建立一个图,先按第一种排名排序, ...

  9. linux 下 .o 文件, .a文件,.so文件的区别

    最近在unbuntu环境下开发代码,由于很少使用linux开发环境,所以对linux编译方面了解更少,关于.o, .a, .so文件和可执行文件一直很困惑 今天特意查了一下关于它们的区分: .o 就相 ...

  10. SharePoint 2010 搜索结果没有显示部分文件

    Why SharePoint 2010 search does not show some results?   SharePoint 2010 search is better than ever ...