获取当前的请求HttpServletRequest,Springboot使用Cookie,生成cookie,获取cookie信息

参考:https://blog.csdn.net/qq_35387940/article/details/83501886

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

作者:把爱放下会走更远
链接:https://www.jianshu.com/p/579ef18709cf
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Springboot使用Cookie,生成cookie,获取cookie信息
参考:https://blog.csdn.net/qq_35387940/article/details/83501886
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值