在自定义弹簧范围中获取当前请求



我在FacesContext Logique上编写了一个自定义弹簧范围,

public class DynamicScope implements Scope{
    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        AbsErpFolder erpFolder = null;
        if (FacesContext.getCurrentInstance()!=null)            
            erpFolder = (AbsErpFolder)FacesUtils.getExternalContext().
getRequestMap().get( ErpFolderKey );
    ............
}  

现在我需要在FileServlet过滤器中使用实例化一个bean。

WebApplicationContext wsc = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
IGenericService erpFileService = (IGenericService) wsc.getBean("erpFileService");

问题是在执行过程中我的 ServletFile 当前 FacesContext 为空(动态范围无法工作),那么如何获取当前请求?

ThreadLocal 应该可以解决问题 (在我的第一个过滤器中定义)

public static ThreadLocal servletRequestHolder  = new ThreadLocal();
public static HttpServletRequest getCurrentRequest(){
    return (HttpServletRequest) servletRequestHolder.get();
}
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
   servletRequestHolder.set( request );
...........
}

相关内容

  • 没有找到相关文章