2014-03-31 279 views
1

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

現在我需要使用

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

的問題是期間實例化在FileServlet過濾豆執行我的ServletFile當前的FacesContext爲null(動態範圍無法工作),那麼如何獲取當前請求呢?

回答

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