Spring 通过类型为 ServletRequestAttributes的 包装纸对象公开当前的 HttpServletRequest对象(以及当前的 HttpSession对象)。此包装器对象绑定到 ThreadLocal,并通过调用 static方法 RequestContextHolder.currentRequestAttributes()获得。
ServletRequestAttributes provides the method getRequest() to get the current request, getSession() to get the current session and other methods to get the attributes stored in both the scopes. The following code, though a bit ugly, should get you the current request object anywhere in the application:
Note that the RequestContextHolder.currentRequestAttributes() method returns an interface and needs to be typecasted to ServletRequestAttributes that implements the interface.
Spring Javadoc: RequestContextHolder 请求上下文持有者 | < a href = “ http://static.springsource.org/Spring/docs/3.0.x/Javadoc-api/org/springFramework/web/context/request/ServletRequestAttributes.html”rel = “ noReferrer”> ServletRequestAttritribute