项目中除了Controller能够接收到请求对象HttpServletRequest和HttpServletResponse外,如果需要在service层使用以上请求和/或响应对象,如果直接从Controller传递过service层感觉太麻烦,而且耦合度太高;那么有什么方式可以优雅的解决该问题么? SpringMvc提供了RequestContextHolder对象!
一、初识RequestContextHolder
RequestContextHolder顾名思义,持有上下文的Request容器~
1、使用以下2个ThreadLocal对象保存RequestAttributes
private static final boolean jsfPresent =
ClassUtils.isPresent("javax.faces.context.FacesContext", RequestContextHolder.class.getClassLoader());
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes");
private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
new NamedInheritableThreadLocal<RequestAttributes>("Request context");
2、获取RequestAttributes、HttpServletRequest、HttpServletResponse
//A.两个方法在没有使用JSF的项目中是没有区别的
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
//RequestContextHolder.getRequestAttributes();
//B1.从request里面获取对应的值
String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_REQUEST);
//B2.从session里面获取对应的值
String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);
//C.获取当前HttpServletRequest、HttpServletResponse
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
疑问:
1)RequestContextHolder获取request和response是怎么和当前请求挂钩的?
2)request和response是什么时候设置进RequestContextHolder?
二、疑问解答
1)RequestContextHolder获取request和response是怎么和当前请求挂钩的?
RequestContextHolder里面有两个ThreadLocal成员属性保存当前线程下的RequestAttributes.
详见以上第一点!
2)request和response是什么时候设置进RequestContextHolder?
HttpServlet继承关系结构:
SpringMVC具有Spring的一些环境变量和Spring容器.类似的XXXAware接口就是对该类提供Spring感知,简单来说就是如果想使用Spring的XXXX就要实现XXXAware,spring会把需要的上下文东西传送过来.
那么剩下要分析的的就是三个类,简单看下源码
1. HttpServletBean 进行初始化工作
2. FrameworkServlet 初始化 WebApplicationContext,并提供service方法预处理请求
3. DispatcherServlet 具体分发处理
那么就可以在FrameworkServlet查看到该类重写了service(),doGet(),doPost()...等方法
/**
* Override the parent class implementation in order to intercept PATCH requests.
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (HttpMethod.PATCH.matches(request.getMethod())) {
processRequest(request, response);
}
else {
super.service(request, response);
}
}
这些实现里面都有一个预处理方法processRequest(request, response),所以定位到了我们要找的位置
查看processRequest(request, response);的实现,具体可以分为三步:
- 获取上一个请求的参数
- 重新建立新的参数
- 设置到XXContextHolder
- 父类的service()处理请求
- 恢复request
- 发布事件
/**
* Process this request, publishing an event regardless of the outcome.
* <p>The actual event handling is performed by the abstract
* {@link #doService} template method.
*/
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
//获取上一次请求保存的LocalContext
LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
LocaleContext localeContext = buildLocaleContext(request); //创建新的LocalContext
//获取上一次保存的RequestAtttibutes
RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
//创建新的RequestAttributes
ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), 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) {
requestAttributes.requestCompleted();
}
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);
}
}
再看创建RequestAttributes的方法buildRequestAttributes
/**
* Build ServletRequestAttributes for the given request (potentially also
* holding a reference to the response), taking pre-bound attributes
* (and their type) into consideration.
* @param request current HTTP request
* @param response current HTTP response
* @param previousAttributes pre-bound RequestAttributes instance, if any
* @return the ServletRequestAttributes to bind, or {@code null} to preserve
* the previously bound instance (or not binding any, if none bound before)
* @see RequestContextHolder#setRequestAttributes
*/
protected ServletRequestAttributes buildRequestAttributes(
HttpServletRequest request, HttpServletResponse response, RequestAttributes previousAttributes) {
if (previousAttributes == null || previousAttributes instanceof ServletRequestAttributes) {
return new ServletRequestAttributes(request, response);
}
else {
return null; // preserve the pre-bound RequestAttributes instance
}
}
把新的RequestAttributes设置进LocalThread,实际上保存的类型为ServletRequestAttributes,这也是为什么在使用的时候可以把RequestAttributes强转为ServletRequestAttributes。
initContextHolders重新设置LocalContextHolder和RequestContextHolder对象成员属性
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()) {
logger.trace("Bound request context to thread: " + request);
}
}
因此RequestContextHolder里面最终保存的为ServletRequestAttributes,这个类相比RequestAttributes方法是多了很多.