java 线程上下文_Java / Spring MVC:向子线程提供请求上下文

我有一个问题,我想将Spring

WebMVC应用程序的某些进程外包到单独的线程中。这很容易并且有效,直到我要使用使用全局请求的类userRightService为止。这在线程中不可用,我们遇到了一个问题,这是可以理解的。

这是我的错误:

java.lang.RuntimeException:

org.springframework.beans.factory.BeanCreationException: Error creating bean

with name 'scopedTarget.userRightsService': 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: Cannot ask for request attribute -

request is not active anymore!

这是我的可运行类:

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)

public class myThread implements Runnable {

private RequestAttributes context;

public DataExportThread(RequestAttributes context) {

this.context = context;

}

public void run() {

RequestContextHolder.setRequestAttributes(context);

并在其中产生:

final DataExportThread dataExportThread =

new myThread(RequestContextHolder.currentRequestAttributes());

final Thread thread = new Thread(myThread);

thread.setUncaughtExceptionHandler((t, e) -> {...});

thread.start();

据我了解,我们将currentRequestAttributes存储在线程中,然后在运行时将它们还原为currentRequestAttributes

…对我来说听起来很可靠,但错误仍然存​​在。我认为为我的案例调整解决方案时犯了一些错误。也许有人可以帮助我找到错误。

在我使用不同的解决方案经历大量的stackoverflow线程之前(请参见下文),因此我可以尝试其他方法,但这对我来说似乎是最清晰,最简单的方法,因此我希望有人可以帮助我找到实现中的错误或解释为什么这是错误的方法。

如果很重要:

4.3.4.RELEASE

顺便说一句:我知道以某种方式重组应用程序会更好,线程中不需要该请求,但是在那种情况下这很复杂,我真的希望我能避免这种情况。

-

编辑1:

无法在线程中创建的Bean是这样启动的:

@Service("userRightsService")

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)

public class UserRightsService {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值