- springmvc在获取Request和Response有很多方式:具体请看:https://www.cnblogs.com/wade-luffy/p/8867144.html
- 产生线程问题的代码如下:
public class BaseController {
protected HttpServletRequest request; protected HttpServletResponse response; protected HttpSession session; @ModelAttribute
public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) { this.request = request; this.response = response; this.session = request.getSession(); }
}
分析:
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container)
throws Exception { while (!this.modelMethods.isEmpty()) {
InvocableHandlerMethod modelMethod = getNextModelMethod(container).getHandlerMethod();
ModelAttribute ann = modelMethod.getMethodAnnotation(ModelAttribute.class);
if (container.containsAttribute(ann.name())) {
if (!ann.binding()) {
container.setBindingDisabled(ann.name());
}
continue;
} Object returnValue = modelMethod.invokeForRequest(request, container);
if (!modelMethod.isVoid()){
String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType());
if (!ann.binding()) {
container.setBindingDisabled(returnValueName);
}
if (!container.containsAttribute(returnValueName)) {
container.addAttribute(returnValueName, returnValue);
}
}
}
}
一, 可以使用其他两种方式来获取request和response对象
public abstract class BaseController { @Autowired
protected HttpServletRequest request;
@Autowired
protected HttpServletResponse response;
}
使用@Autowired或者@Resource注解注入,是线程安全的,当用户发出请求后,会经过FrameworkServlet中的processRequest()方法做了一些骚操作,然后再交给子类DispatcherServlet中的doService()去处理这个请求。这些骚操作就包括把request,response对象包装成ServletRequestAttributes对象,然后放入到ThreadLocal中,看到ThreadLocal就明白了
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes"); private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
new NamedInheritableThreadLocal<RequestAttributes>("Request context");
private static ServletRequestAttributes currentRequestAttributes() {
RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
if (!(requestAttr instanceof ServletRequestAttributes)) {
throw new IllegalStateException("Current request is not a servlet request");
}
return (ServletRequestAttributes) requestAttr;
}
Autowired注入流程结合上面代码:@Autowired注入HttpServletRequest对象的过程。这里以HttpServletRequest对象注入举例。首先调用DefaultListableBeanFactory中的findAutowireCandidates()方法,判断autowiringType类型是否和requiredType类型一致或者是autowiringType是否是requiredType的父接口(父类)。如果满足条件的话,我们会从resolvableDependencies中通过autowiringType(对应着上文的ServletRequest)拿到autowiringValue(对应着上文的RequestObjectFactory)。然后调用AutowireUtils.resolveAutowiringValue()对我们的ObjectFactory进行处理。
二, 简单粗暴不会产生任何问题:直接在需要request和response对象得方法参数注入即可,有其他参数直接接着在后面写,即可,这个方法参数,作用域在方法,不会产生线程问题
@RequestMapping(value = "/cms/preview/{id}",method = RequestMethod.GET) public void preview(HttpServletResponse response, @PathVariable("id") String id) { }
具体详细请看:https://blog.csdn.net/weixin_33768481/article/details/88303335
里面也介绍了进行压力测试模拟高并发请求
解决方案:
现在改成如下代码,则是可以的!
改动:
1 增加了@Autowired
2 设置curUser为private,并且封装成属性:getCurUser()
public class SuperBaseController {
@Autowired
protected HttpServletRequest request;
@Autowired
protected HttpServletResponse response;
private SysUser curUser;
@Value("${isDebug}")
protected Boolean isDebug; @ModelAttribute
public void setLang(HttpServletRequest request, HttpServletResponse response) {
this.sysHostUrl = this.request.getAttribute(ComContants.SYSHOSTURL).toString();
this.sysHost = this.request.getAttribute(ComContants.SYSHOST).toString();
} public SysUser getCurUser() {
curUser=WebUtil.getCurrentUser(isDebug, request);
return curUser;
} }
转自1:https://blog.csdn.net/hanjun0612/article/details/103421903
转自2: https://blog.csdn.net/hanjun0612/article/details/103427040