简体   繁体   English

Spring 引导:无法访问 Spring 调度程序中的请求 scope bean

[英]Spring Boot: Unable to access the request scope bean in Spring Scheduler

In my Spring Boot application, i've a Scheduler task which executes for every one hour.在我的 Spring 启动应用程序中,我有一个每隔一小时执行一次的调度程序任务。 In the scheduler method trying to access the request-scope bean.在尝试访问请求范围 bean 的调度程序方法中。 Always getting the exception org.springframework.beans.factory.BeanCreationException .总是得到异常org.springframework.beans.factory.BeanCreationException

Here is the code sample.这是代码示例。

@Data
public class TestVo {
  private String message = "Hello";
}

@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
public TestVo testVo() {
    return new TestVo();
}

Accessing the above created bean in scheduler method as below,在调度程序方法中访问上面创建的 bean,如下所示,

@Autowired
private TestVo testVo;

@Scheduled(cron="0 0 * * * *")
public void greetings() {
  System.out.println(testVo.getMessage()); // accessing request scope bean
}

getting below exception with above code,使用上述代码获得以下异常,

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.scheduledJobTaskExecutor': Scope 'request' is not active for the current thread; org.springframework.beans.factory.BeanCreationException:创建名为'scopedTarget.scheduledJobTaskExecutor'的bean时出错:Scope'request'对于当前线程无效; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton;如果您打算从 singleton 引用它,请考虑为此 bean 定义一个范围代理; 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?嵌套异常是 java.lang.IllegalStateException:未找到线程绑定请求:您是指实际 web 请求之外的请求属性,还是在原始接收线程之外处理请求? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.如果您实际上是在 web 请求中操作并且仍然收到此消息,则您的代码可能在 DispatcherServlet 之外运行:在这种情况下,使用 RequestContextListener 或 RequestContextFilter 来公开当前请求。

will be helpful.. thanks.会有帮助的..谢谢。

The request scoped beans are bounded to specific requests.请求范围的 bean 绑定到特定的请求。 Every time a request comes, a new instance will be created and after the request finished it will be destroyed.每次请求到来时,都会创建一个新实例,并在请求完成后将其销毁。 The request is bounded to a thread and use that thread to process the request (in non reactive environment).请求绑定到一个线程并使用该线程来处理请求(在非反应环境中)。 Even if it was possible, the scheduler wouldnt know, which request object it should use in this situation.即使有可能,调度程序也不知道在这种情况下应该使用哪个请求 object。 Consider you have 100 active request when the scheduled job starts to run, how it should choose one?假设您在计划作业开始运行时有 100 个活动请求,应该如何选择一个? Or if there arent any active request (so no instance hold by the context?).或者如果没有任何活动请求(所以上下文没有实例保存?)。 You can inject request scope into singleton via proxy because the singleton method call will be handled on the same request thread, but the scheduled job use its own thread pool, which not bounded to any requests.您可以通过代理将请求 scope 注入到 singleton 中,因为 singleton 方法调用将在同一个请求线程上处理,但计划的作业使用它自己的线程池,它不绑定到任何请求。 Maybe now you can see the problem using request scoped bean in the scheduler.也许现在您可以在调度程序中使用请求范围的 bean 看到问题。 If you want to use the same logic in the scheduler and in request scoped beans, you can for example extract it into a superclass.如果您想在调度程序和请求范围的 bean 中使用相同的逻辑,您可以例如将其提取到超类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM