15
votes

Currently experimenting reactive programming with Spring Boot 2.0.0.M4, Spring 5.0.0.RC4 and Reactor 3.1.0.RC1.

Injecting a @RequestScope or @SessionScope bean into a WebFlux REST controller fails at runtime:

java.lang.IllegalStateException: No Scope registered for scope name 'request'
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:342) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.0.RC4.jar:5.0.0.RC4]
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35) ~[spring-aop-5.0.0.RC4.jar:5.0.0.RC4]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-5.0.0.RC4.jar:5.0.0.RC4]
    at com.adeliosys.scope.Counter$$EnhancerBySpringCGLIB$$7dce0361.increment(<generated>) ~[classes/:na]
    at com.adeliosys.scope.Controller.getQuote(Controller.java:25) ~[classes/:na] // line with theScopedBean.doSometing() call in the REST controller
    (...)

The blocking equivalent with Spring Web MVC works fine.

I understand that reactive programming messes with thread locals, but is request or session scoped beans injection supported by WebFlux ?

If not, is this planed ?

Thank you for your time.

1

1 Answers

13
votes

Indeed, thread locals can't be used in a Spring WebFlux application, because units of work can happen on any thread at any time, and you can't expect a request to be processed on a single thread.

That type of feature could be implemented using the new Reactor Context, which allows you to attach some data to a reactive pipeline. As you've noticed, this feature is not supported currently in Spring WebFlux.