public class ReactiveLazyContextVariable extends org.thymeleaf.context.LazyContextVariable<Object> implements org.thymeleaf.context.ILazyContextVariable<Object>
Implementation of the ILazyContextVariable
interface meant to contain
a reactive data stream that should not be resolved until view is rendered.
By being added to the context/model wrapped by an object of this class, reactive asynchronous objects will reach the execution phase of the view layer unresolved, and will only be resolved if they are really needed. So asynchronous variables that the template does not really need in the end (because template logic resolves in a way that doesn't make use of them) will never be consumed at all.
Note that resolving this kind of objects means actually blocking and collecting its values.
The reactive async object wrapped by this class will usually have the shape of an implementation of the
Publisher
interface, such as Flux
. But other types of reactive
artifacts are supported thanks to Spring's ReactiveAdapterRegistry
mechanism.
When lazily resolving these variables, this class mirrors the mechanism used by Spring for resolving asynchronous variables at the model of views:
Being multi-valued does not mean to necessarily return more than one value,
but simply to have the capability to do so. E.g. a Flux
object will
be considered multi-valued even if it publishes none or just one result, whereas a
Mono
object will be considered single-valued.
Example use:
@RequestMapping("/something")
public String doSomething(final Model model) {
final Publisher<Item> async = ...;
// If 'async' is multi-valued, 'someData' will be usable as if it were of type List<Item>
// If 'async' is single-valued, 'someData' will be usable as if it were of type Item
model.addAttribute("someData", new ReactiveLazyContextVariable(async));
return "view";
}
This class is NOT thread-safe. Thread-safety is not a requirement for context variables.
ILazyContextVariable
Constructor and Description |
---|
ReactiveLazyContextVariable(Object asyncObject)
Creates a new lazy context variable, wrapping a reactive asynchronous object.
|
Modifier and Type | Method and Description |
---|---|
protected Object |
loadValue() |
void |
setReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry reactiveAdapterRegistry)
Sets the
ReactiveAdapterRegistry used for converting (if necessary) the wrapped asynchronous
object into a Publisher . |
public ReactiveLazyContextVariable(Object asyncObject)
Creates a new lazy context variable, wrapping a reactive asynchronous object.
The specified asyncObject must be adaptable to a Reactive Streams
Publisher
by means of Spring's ReactiveAdapterRegistry
mechanism. If no
adapter has been registered for the type of the asynchronous object, and exception will be
thrown during lazy resolution.
Examples of supported implementations are Reactor's Flux
and Mono
, and also
RxJava's Observable and Single.
asyncObject
- the asynchronous object, which must be convertible to a Publisher
by
means of Spring's ReactiveAdapterRegistry
.public final void setReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry reactiveAdapterRegistry)
Sets the ReactiveAdapterRegistry
used for converting (if necessary) the wrapped asynchronous
object into a Publisher
.
This method is transparently called before template execution in order
to initialize lazy context variables. It can also be called programmatically, but there is normally
no reason to do this. If not called at all, only Flux
and Mono
will be allowed as valid types
for the wrapped asynchronous object.
reactiveAdapterRegistry
- the reactive adapter registry.Copyright © 2017 The THYMELEAF team. All rights reserved.