Using a request scoped bean outside of an actual web request

前端 未结 5 808
我在风中等你
我在风中等你 2020-12-09 15:39

I have a web application that has a Spring Integration logic running with it in a separated thread. The problem is that at some point my Spring Integration logic tries to us

5条回答
  •  悲&欢浪女
    2020-12-09 16:03

    You could publish the request in the new Thread like this:

    import org.springframework.web.context.request.RequestContextListener;
     ...
    ServletRequestEvent requestEvent = new ServletRequestEvent(req.getServletContext(), req);
    RequestContextListener requestContextListener = new RequestContextListener();
    requestContextListener.requestInitialized(requestEvent);
     ...
    requestContextListener.requestDestroyed(requestEvent);
    

    If you look inside requestInitialized()-method you will find a ThreadLocal-variable holding the request. Now you can autowire your request successfully.

提交回复
热议问题