RequestContextHolder
RequestContextHolder
顾名思义,持有上下文的Request
容器.
SpringMVC 获取request
和response
1 2 3 4 5 6 7 8
| RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest(); HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
|
一次请求,Web应用服务器就会分配一个线程去处理。也就是说,在Service方法中获取到的HttpServletRequest对象需要满足:线程内共享,线程间隔离。
这恰恰是ThreadLocal的应用场景。
###利用ServletRequestListener实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpServletRequest;
public class RequestHolder implements ServletRequestListener {
private static ThreadLocal<HttpServletRequest> httpServletRequestHolder = new ThreadLocal<HttpServletRequest>(); @Override public void requestInitialized(ServletRequestEvent requestEvent) { HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest(); httpServletRequestHolder.set(request); } @Override public void requestDestroyed(ServletRequestEvent requestEvent) { httpServletRequestHolder.remove(); } public static HttpServletRequest getHttpServletRequest() { return httpServletRequestHolder.get(); } }
|
利用Filter实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| import java.io.IOException;
import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest;
public class RequestHolder implements Filter {
private static ThreadLocal<HttpServletRequest> httpServletRequestHolder = new ThreadLocal<HttpServletRequest>();
@Override public void init(FilterConfig filterConfig) throws ServletException { }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { httpServletRequestHolder.set((HttpServletRequest) request); try { chain.doFilter(request, response); } catch (Exception e) { throw e; } finally { httpServletRequestHolder.remove(); } }
@Override public void destroy() { }
public static HttpServletRequest getHttpServletRequest() { return httpServletRequestHolder.get(); } }
|
利用SpringMVC拦截器实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class RequestHolder extends HandlerInterceptorAdapter {
private static ThreadLocal<HttpServletRequest> httpServletRequestHolder = new ThreadLocal<HttpServletRequest>(); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { httpServletRequestHolder.set(request); return true; } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { httpServletRequestHolder.remove(); } public static HttpServletRequest getHttpServletRequest() { return httpServletRequestHolder.get(); } }
|