Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session and request scopes with Spring WebFlux

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.

like image 514
Florian Beaufumé Avatar asked Oct 03 '17 09:10

Florian Beaufumé


People also ask

What is the difference between request scope and session scope?

In request scope, a bean is defined to an HTTP request whereas in session scope, it is scoped to an HTTP session. So for an instance, if the bean scope is request and, a user makes more than one request for a web page in his user session, then on every request a new bean would be created.

What is request scope in spring?

A request-scoped bean is an object managed by Spring, for which the framework creates a new instance for every HTTP request. The app can use the instance only for the request that created it. Any new HTTP request (from the same or other clients) creates and uses a different instance of the same class (figure 2).

Can I use spring MVC and WebFlux together?

both infrastructure will compete for the same job (for example, serving static resources, the mappings, etc) mixing both runtime models within the same container is not a good idea and is likely to perform badly or just not work at all.

How does spring WebFlux work internally?

What is Spring WebFlux ? Spring WebFlux is parallel version of Spring MVC and supports fully non-blocking reactive streams. It support the back pressure concept and uses Netty as inbuilt server to run reactive applications. If you are familiar with Spring MVC programming style, you can easily work on webflux also.


1 Answers

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.

like image 116
Brian Clozel Avatar answered Sep 19 '22 23:09

Brian Clozel