spring HttpServletRequest 简介

前提:工作遇到controller中通过注解的方式注入

@Resource
private HttpServletRequest request;

我们都知道spring 默认是单例,当遇到并发的时候线程不安全,但是经过测试它安全的。

综上,确定spring肯定对它做了处理,因此打开我的探索之路.....

上源码

public class RequestContextListener implements ServletRequestListener {
private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES";

public RequestContextListener() {
}

public void requestInitialized(ServletRequestEvent requestEvent) {
if(!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
throw new IllegalArgumentException("Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
} else {
HttpServletRequest request = (HttpServletRequest)requestEvent.getServletRequest();
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
LocaleContextHolder.setLocale(request.getLocale());
        //放到这里了,RequestContextHolder下的requestAttributes让我们跟进去瞅瞅
RequestContextHolder.setRequestAttributes(attributes);
}
}

----------------------------------------------------------------------------------------------------------------
public abstract class RequestContextHolder {
private static final boolean jsfPresent = ClassUtils.isPresent("javax.faces.context.FacesContext", RequestContextHolder.class.getClassLoader());
  //原来如此啊,是通过threadLocal去保证的
private static final ThreadLocal<RequestAttributes> requestAttributesHolder = new NamedThreadLocal("Request attributes");
private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder = new NamedInheritableThreadLocal("Request context");

public RequestContextHolder() {
}






posted @ 2016-12-05 10:42  蝼蚁尚不自知  阅读(1323)  评论(0编辑  收藏  举报