SpringMVC之RequestContextHolder分析

SpringMVC对应的controller方法中,如何获取request和response。一种是作为参数传递进来,一种是使用SpringMVC提供的RequestContextHolder去获取。例如下面代码

@RequestMapping("/helloResponseBody")
    @ResponseBody
    public String helloResponseBody(){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        logger.info("request = " + request);
        return UUID.randomUUID().toString();
    }

 

这里,主要是介绍RequestContextHolder的原理,假定使用SpringMVC提供的标准的servlet类DispatcherServlet,容器是Tomcat。

 

 

 


package org.springframework.web.context.request;


只保留关键代码
public abstract class RequestContextHolder  {

	private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
			new NamedThreadLocal<>("Request attributes");

	public static void setRequestAttributes(@Nullable RequestAttributes attributes) {
		setRequestAttributes(attributes, false);
	}
	
	public static void setRequestAttributes(@Nullable RequestAttributes attributes, boolean inheritable) {
            ......
			requestAttributesHolder.set(attributes);
		    ......
		}
	}

	@Nullable
	public static RequestAttributes getRequestAttributes() {
		RequestAttributes attributes = requestAttributesHolder.get();
        ......
		return attributes;
	}
    ......
}

 

 

 

package org.springframework.web.servlet;


只保留关键代码
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
	
	/**
	 * Close the WebApplicationContext of this servlet.
	 * @see org.springframework.context.ConfigurableApplicationContext#close()
	 */
	@Override
	public void destroy() {
		......
	}

	/**
	 * Override the parent class implementation in order to intercept PATCH requests.
	 */
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
		if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
			processRequest(request, response);
		}
		else {
			super.service(request, response);
		}
	}
	
	@Override
	protected final void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		processRequest(request, response);
	}

	@Override
	protected final void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
	
	@Override
	protected final void doPut(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
	
	@Override
	protected final void doDelete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
	
	@Override
	protected void doOptions(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
	
	@Override
	protected void doTrace(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * 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 {
		LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
		LocaleContext localeContext = buildLocaleContext(request);

		RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
		ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);

		initContextHolders(request, localeContext, requestAttributes);

		try {
			doService(request, response);
		}
		catch (ServletException | IOException ex) {
			failureCause = ex;
			throw ex;
		}
		catch (Throwable ex) {
			failureCause = ex;
			throw new NestedServletException("Request processing failed", ex);
		}

	}
	
	@Nullable
	protected ServletRequestAttributes buildRequestAttributes(HttpServletRequest request,
			@Nullable HttpServletResponse response, @Nullable RequestAttributes previousAttributes) {

		if (previousAttributes == null || previousAttributes instanceof ServletRequestAttributes) {
			return new ServletRequestAttributes(request, response);
		}
		else {
			return null;  // preserve the pre-bound RequestAttributes instance
		}
	}

	private void initContextHolders(HttpServletRequest request,
			@Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) {

		if (requestAttributes != null) {
			RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
		}
	}

	/**
	 * Subclasses must implement this method to do the work of request handling,
	 * receiving a centralized callback for GET, POST, PUT and DELETE.
	 * <p>The contract is essentially the same as that for the commonly overridden
	 * {@code doGet} or {@code doPost} methods of HttpServlet.
	 * <p>This class intercepts calls to ensure that exception handling and
	 * event publication takes place.
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @throws Exception in case of any kind of processing failure
	 * @see javax.servlet.http.HttpServlet#doGet
	 * @see javax.servlet.http.HttpServlet#doPost
	 */
	protected abstract void doService(HttpServletRequest request, HttpServletResponse response)
			throws Exception;

}

 

 

核心思想,每次http请求,容器最终都会调用DispatcherServlet类的service方法,实际调用的是DispatcherServlet父类FrameworkServlet的service方法。FrameworkServlet的service方法内,可能直接调用processRequest方法,也可能调用super.service方法。即使调用父类的service方法,父类service方法最终还会调用FrameworkServlet类的doPost、doGet等相应的方法(理由见下文备注),最终结论是,无论何种形式,最终都会调用FrameworkServlet类的processRequest方法。

processRequest方法内部,会先将request和response对象封装好,然后将封装好的对象,绑定到RequestContextHolder类的ThreadLocal<RequestAttributes> requestAttributesHolder属性内。之后FrameworkServlet类的processRequest方法,调用DispatcherServlet类的doService方法。DispatcherServlet类的doService方法内部,调用DispatcherServlet类的doDispatch方法,进而走完整个请求。

 

简单的说,就是SpringMVC利用ThreadLocal技术,每次在处理请求前,将请求的request和response放到RequestContextHolder的ThreadLocal对象内。在后续处理请求时,可以通过RequestContextHolder类获取ThreadLocal内的request和response对象。

 

备注:这需要巩固一个知识点:当子类重写了父类的函数,那么子类的对象如果调用该函数,一定调用的是重写过后的函数。可以通过super关键字进行父类的重写函数的调用。

public class ATest extends AATest{
    protected void doGet(){
        System.out.println("ATest doGet");
    }

    public static void main(String[] args) {
        new ATest().service();
    }
}

abstract class AATest extends AAATest{
    protected void service(){
        System.out.println("AATest service");
        doGet();
    }

    protected void doGet(){
        System.out.println("AATest doGet");
    }
}


结论:
AATest service
ATest doGet

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值