SpringMVC之RequestContextHolder分析
最近遇到的问题是在service获取request和response,正常来说在service层是没有request的,然⽽直接从controlller传过来的话解决⽅法太粗暴,后来发现了SpringMVC提供的RequestContextHolder遂去分析⼀番,并借此对SpringMVC的结构深⼊了解⼀下,后⾯会再发⽂章详细分析源码
1.RequestContextHolder的使⽤
RequestContextHolder顾名思义,持有上下⽂的Request容器.使⽤是很简单的,具体使⽤如下:
//两个⽅法在没有使⽤JSF的项⽬中是没有区别的
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
//RequestAttributes();
//从session⾥⾯获取对应的值
String str = (String) Attribute("name",RequestAttributes.SCOPE_SESSION);
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
看到这⼀般都会想到⼏个问题:
1. request和response怎么和当前请求挂钩?
2. request和response等是什么时候设置进去的?
2.解决疑问
2.1 request和response怎么和当前请求挂钩?
⾸先分析RequestContextHolder这个类,⾥⾯有两个ThreadLocal保存当前线程下的request,关于ThreadLocal可以参考我的另⼀篇博⽂[]
//得到存储进去的request
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes");
//可被⼦线程继承的request
private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
new NamedInheritableThreadLocal<RequestAttributes>("Request context");
再看`getRequestAttributes()`⽅法,相当于直接获取ThreadLocal⾥⾯的值,这样就保证了每⼀次获取到的Request是该请求的request.
public static RequestAttributes getRequestAttributes() {
RequestAttributes attributes = ();
if (attributes == null) {
attributes = ();
}
return attributes;
}
2.2request和response等是什么时候设置进去的?
这个的话需要对springMVC结构的`DispatcherServlet`的结构有⼀定了解才能准确的定位该去哪⾥相关代码.
在IDEA中会显⽰如下的继承关系.
左边1这⾥是Servlet的接⼝和实现类.
右边2这⾥是使得SpringMVC具有Spring的⼀些环境变量和Spring容器.类似的XXXAware接⼝就是对该类提供Spring感知,简单来说就是如果想使⽤Spring的XXXX就要实现XXXAware,spring会把需要的东西传送过来.
那么剩下要分析的的就是三个类,简单看下源码
1. HttpServletBean 进⾏初始化⼯作
2. FrameworkServlet 初始化 WebApplicationContext,并提供service⽅法预处理请
3. DispatcherServlet 具体分发处理.
那么就可以在FrameworkServlet查看到该类重写了service(),doGet(),doPost()...等⽅法,这些实现⾥⾯都有⼀个预处理⽅法
`processRequest(request, response);`,所以定位到了我们要的位置
查看`processRequest(request, response);`的实现,具体可以分为三步:
1. 获取上⼀个请求的参数
2. 重新建⽴新的参数
3. 设置到XXContextHolder
4. ⽗类的service()处理请求
5. 恢复request
6. 发布事
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {spring framework是什么框架的
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
//获取上⼀个请求保存的LocaleContext
LocaleContext previousLocaleContext = LocaleContext();
//建⽴新的LocaleContext
LocaleContext localeContext = buildLocaleContext(request);
//获取上⼀个请求保存的RequestAttributes
RequestAttributes previousAttributes = RequestAttributes();
//建⽴新的RequestAttributes
ServletRequestAttributes requestAttributes = buildRequestAttributes(request,
response, previousAttributes);
WebAsyncManager asyncManager = AsyncManager(request);
new RequestBindingInterceptor());
//具体设置的⽅法
initContextHolders(request, localeContext, requestAttributes);
try {
doService(request, response);
}
catch (ServletException ex) {
failureCause = ex;
throw ex;
}
catch (IOException ex) {
failureCause = ex;
throw ex;
}
catch (Throwable ex) {
failureCause = ex;
throw new NestedServletException("Request processing failed", ex);
}
finally {
//恢复
resetContextHolders(request, previousLocaleContext, previousAttributes);
if (requestAttributes != null) {
}
if (logger.isDebugEnabled()) {
if (failureCause != null) {
this.logger.debug("Could not complete request", failureCause);
}
else {
if (asyncManager.isConcurrentHandlingStarted()) {
logger.debug("Leaving response open for concurrent processing");
}
else {
this.logger.debug("Successfully completed request");
}
}
}
//发布事件
publishRequestHandledEvent(request, response, startTime, failureCause);
}
}
再看initContextHolders(request, localeContext, requestAttributes)⽅法,把新的RequestAttributes设置进LocalThread,实际上保存的类型为ServletRequestAttributes,这也是为什么在使⽤的时候可以把RequestAttributes强转为ServletRequestAttributes.
private void initContextHolders(HttpServletRequest request,
LocaleContext localeContext,
RequestAttributes requestAttributes) {
if (localeContext != null) {
LocaleContextHolder.setLocaleContext(localeContext,
this.threadContextInheritable);
}
if (requestAttributes != null) {
RequestContextHolder.setRequestAttributes(requestAttributes,
this.threadContextInheritable);
}
if (logger.isTraceEnabled()) {
}
}
因此RequestContextHolder⾥⾯最终保存的为ServletRequestAttributes,这个类相⽐`RequestAttributes`⽅法是多了很多.
项⽬⽰例可以参考: SSM框架整合:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论