在我的應(yīng)用程序中,我有一些異步Web服務(wù)。服務(wù)器接受請(qǐng)求,返回OK響應(yīng)并使用AsyncTaskExecutor啟動(dòng)處理請(qǐng)求。我的問題是如何在此處啟用請(qǐng)求范圍,因?yàn)樵诖颂幚碇形倚枰@取注釋的類:@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)現(xiàn)在我得到例外:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.requestContextImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.因?yàn)樗\(yùn)行SimpleAsyncTaskExecutor而不是運(yùn)行DispatcherServlet我的異步處理請(qǐng)求taskExecutor.execute(new Runnable() { @Override public void run() { asyncRequest(request); }});taskExecutor的位置是:<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
3 回答

小唯快跑啊
TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
由于原始父請(qǐng)求處理線程可能已將響應(yīng)提交到客戶端并且所有請(qǐng)求對(duì)象都已銷毀,因此無法在子異步線程中獲取請(qǐng)求范圍對(duì)象。處理此類方案的一種方法是使用自定義范圍,如SimpleThreadScope。
SimpleThreadScope的一個(gè)問題是子線程不會(huì)繼承父范圍變量,因?yàn)樗趦?nèi)部使用簡(jiǎn)單的ThreadLocal。要克服該實(shí)現(xiàn)自定義范圍,它與SimpleThreadScope完全相似,但在內(nèi)部使用InheritableThreadLocal。有關(guān)更多信息,請(qǐng)參閱 Spring MVC:如何在生成的線程中使用請(qǐng)求范圍的bean?
添加回答
舉報(bào)
0/150
提交
取消