gpt4 book ai didi

java - 获取自定义 Spring 作用域中的当前请求

转载 作者:太空宇宙 更新时间:2023-11-04 06:55:40 25 4
gpt4 key购买 nike

我基于 FacesContext Logique 编写了一个自定义 spring 作用域,

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(动态作用域无法工作),那么如何获取当前请求?

最佳答案

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

关于java - 获取自定义 Spring 作用域中的当前请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22768063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com