1

I have the spring mvc app which works great. I have a bean X which has the following declaration

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

I need to create a background cron job. I have added a threadPool and @Scheduled annotation to my new bean which is using dependency bean X.

When my cron task is started by Spring I get this exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.X': 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.

I understand that my bean X will be injected only by DispatcherServlet.

My question is how to fix this behaviour?

Yev
  • 337
  • 4
  • 19
  • 1
    You don't. `@Scheduled` is meant to run on a thread separate from the request handling threads. It'll never have access to request scoped beans because it's unrelated to those requests. – Sotirios Delimanolis Dec 06 '15 at 16:51
  • Any way to inject my X bean to spring context used by thread pool by `BeanPostProcessor` hooks or something like that? I tried this http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-other-injection, but no chance – Yev Dec 06 '15 at 16:56
  • It doesn't seem like you understand the lifecycle of a request scoped bean. See [here](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-request). – Sotirios Delimanolis Dec 06 '15 at 17:03
  • I understand it. Spring can manage dependency resolving for beans with different scope. Do you know something about `scoped-proxy`? – Yev Dec 06 '15 at 17:09
  • 1
    Go read my answer and others, [here](http://stackoverflow.com/a/33115128/438154). It directly addresses the session scope, but the same applies to the request scope. – Sotirios Delimanolis Dec 06 '15 at 17:11
  • Seems your answer does not contain any link to the official documentation. This citation is from the official spring doc: `The Spring IoC container manages not only the instantiation of your objects (beans), but also the wiring up of collaborators (or dependencies). If you want to inject (for example) an HTTP request scoped bean into another bean of a longer-lived scope, you may choose to inject an AOP proxy in place of the scoped bean. That is, you need to inject a proxy object that exposes the same public interface as the scoped object but that can also retrieve` – Yev Dec 06 '15 at 17:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97127/discussion-between-yev-and-sotirios-delimanolis). – Yev Dec 06 '15 at 17:18

0 Answers0