Spring 中使用自定义的 ThreadLocal 存储导致的坑_第1页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、spring 中使用自定义的 threadlocal 存储导致的坑spring 中有时候我们需要存储一些和 request 相关联的变量,例如用户的登陆有关信息等,它的生命周期和 request 相同。一个简单想到的实现方法是用法 threadlocal:public class securitycontextholder private static final threadlocal securitycontext = new threadlocal();public static void set(securitycontext context) securitycontext.set(

2、context);public static securitycontext get() return securitycontext.get();public static void clear() securitycontext.remove();用法一个自定义的 handlerinterceptor 将有关信息注入进去:slf4jcomponentpublic class requestinterceptor implements handlerinterceptor overridepublic boolean prehandle(httpservletrequest request,

3、 httpservletresponse response, object handler) throwsexception try securitycontextholder.set(retrieverequestcontext(request); catch (exception ex) log.warn("读取哀求信息失败", ex);return true;overridepublic void posthandle(httpservletrequest request, httpservletresponse response, object ha

4、ndler, nullablemodelandview modelandview) throws exception securitycontextholder.clear();通过这样,我们就可以在 controller 中挺直用法这个 context,很便利的猎取到有关用户的信息:slf4jrestcontrollerclass controller public result get() long userid = securitycontextholder.get()。getuserid();/这个办法也是无数博客中用法的。然而这个办法却存在着一个很隐蔽的坑: handlerinter

5、ceptor 的 posthandle 并不总是会调用。当 controller 中浮现 exception:slf4jrestcontrollerclass controller public result get() long userid = securitycontextholder.get()。getuserid();/throw new runtimeexception();或者在 handlerinterceptor 的 prehandle 中浮现 exception:slf4jcomponentpublic class requestinterceptor implements

6、 handlerinterceptor overridepublic boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throwsexception try securitycontextholder.set(retrieverequestcontext(request); catch (exception ex) log.warn("读取哀求信息失败", ex);/throw new runtimeexception();

7、/return true;这些状况下, posthandle 并不会调用。这就导致了 threadlocal 变量不能被清理。在平时的 java 环境中,threadlocal 变量随着 thread 本身的销毁,是可以被销毁掉的。但 spring 因为采纳了线程池的设计,响应哀求的线程可能会向来常驻,这就导致了变量向来不能被 gc 回收。更糟糕的是,这个没有被正确回收的变量,因为线程池对线程的复用,可能会串到别的 request 当中,进而挺直导致代码规律的错误。为了解决这个问题,我们可以用法 spring 自带的 requestcontextholder ,它背后的原理也是 threadl

8、ocal,不过它总会被更底层的 servlet 的 filter 清理掉,因此不存在泄露的问题。下面是一个用法 requestcontextholder 重写的例子:public class securitycontextholder private static final string security_context_attributes = "security_context"public static void setcontext(securitycontext context) requestcontextholder.currentreque

9、stattributes()。setattribute(security_context_attributes,context,requestattributes.scope_request);public static securitycontext get() return (securitycontext)requestcontextholder.currentrequestattributes()。getattribute(security_context_attributes, requestattributes.scope_request);除了用法 requestcontextholder 还可以用法 reque

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论