【问题标题】:How can I use ApplicationContext session scoped classes from a different thread如何使用来自不同线程的 ApplicationContext 会话范围类
【发布时间】:2020-02-01 08:28:45
【问题描述】:

使用 @Async 注释我想在不同的线程中调用一个方法,该线程可以访问 Session 和 Request 范围的类。

但是,当 ApplicationContext 尝试获取 bean 时,会生成以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.sessionInfoClass': Scope 'session' is not active for the current thread;

我曾尝试扩展 ApplicationContextAware 类来保存主线程上下文。

我也尝试过这个问题How to enable request scope in async task executor的建议解决方案

源码在 Github

https://github.com/saavedrah/spring-threadSample

【问题讨论】:

    标签: java spring-boot threadpool


    【解决方案1】:

    我已经为你的 repo 创建了一个 pull request 来解决这个问题。

    基本上,我将this solution 也扩展为Runnable 案例。

    要验证它,运行ThreadSampleApplication 类然后点击http://localhost:8080/testAsync

    【讨论】:

    • 谢谢 Eugen,我使用 Url 进行了检查,它有效,我会将其标记为已回答。另外你知道如何修复 RunnableServiceTest 吗?
    • 您需要使用WebClient 来访问相同的网址。一个非常简单的例子见dzone.com/articles/spring-boot-webclient-and-its-testing
    猜你喜欢
    • 2011-01-30
    • 2018-08-02
    • 2021-12-03
    • 2020-01-23
    • 2019-11-08
    • 2023-03-04
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多