在Custom Spring Scope中获取当前请求

时间:2014-03-31 17:18:53

标签: java spring jsf servlets request

我在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 Filter中实例化一个bean
WebApplicationContext wsc = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
IGenericService erpFileService = (IGenericService) wsc.getBean("erpFileService");

问题是在执行期间我的ServletFile当前的FacesContext为null(动态范围无法工作)所以如何获取当前请求?

1 个答案:

答案 0 :(得分:1)

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 );
...........
}