@@ -93,7 +93,8 @@ class ReactiveTypeHandler {
93
93
94
94
private final ContentNegotiationManager contentNegotiationManager ;
95
95
96
- private final ContextSnapshotFactory contextSnapshotFactory ;
96
+ @ Nullable
97
+ private final Object contextSnapshotHelper ;
97
98
98
99
99
100
public ReactiveTypeHandler () {
@@ -102,16 +103,23 @@ public ReactiveTypeHandler() {
102
103
103
104
ReactiveTypeHandler (
104
105
ReactiveAdapterRegistry registry , TaskExecutor executor , ContentNegotiationManager manager ,
105
- @ Nullable ContextSnapshotFactory contextSnapshotFactory ) {
106
+ @ Nullable Object contextSnapshotFactory ) {
106
107
107
108
Assert .notNull (registry , "ReactiveAdapterRegistry is required" );
108
109
Assert .notNull (executor , "TaskExecutor is required" );
109
110
Assert .notNull (manager , "ContentNegotiationManager is required" );
110
111
this .adapterRegistry = registry ;
111
112
this .taskExecutor = executor ;
112
113
this .contentNegotiationManager = manager ;
113
- this .contextSnapshotFactory = (contextSnapshotFactory != null ?
114
- contextSnapshotFactory : ContextSnapshotFactory .builder ().build ());
114
+ this .contextSnapshotHelper = initContextSnapshotHelper (contextSnapshotFactory );
115
+ }
116
+
117
+ @ Nullable
118
+ private static Object initContextSnapshotHelper (@ Nullable Object snapshotFactory ) {
119
+ if (isContextPropagationPresent ) {
120
+ return new ContextSnapshotHelper ((ContextSnapshotFactory ) snapshotFactory );
121
+ }
122
+ return null ;
115
123
}
116
124
117
125
@@ -140,8 +148,10 @@ public ResponseBodyEmitter handleValue(Object returnValue, MethodParameter retur
140
148
141
149
TaskDecorator taskDecorator = null ;
142
150
if (isContextPropagationPresent ) {
143
- returnValue = ContextSnapshotHelper .writeReactorContext (returnValue , this .contextSnapshotFactory );
144
- taskDecorator = ContextSnapshotHelper .getTaskDecorator (this .contextSnapshotFactory );
151
+ ContextSnapshotHelper helper = (ContextSnapshotHelper ) this .contextSnapshotHelper ;
152
+ Assert .notNull (helper , "No ContextSnapshotHelper" );
153
+ returnValue = helper .writeReactorContext (returnValue );
154
+ taskDecorator = helper .getTaskDecorator ();
145
155
}
146
156
147
157
ResolvableType elementType = ResolvableType .forMethodParameter (returnType ).getGeneric ();
@@ -534,25 +544,31 @@ public ResolvableType getReturnType() {
534
544
}
535
545
536
546
537
- private static class ContextSnapshotHelper {
547
+ private static final class ContextSnapshotHelper {
548
+
549
+ private final ContextSnapshotFactory snapshotFactory ;
550
+
551
+ private ContextSnapshotHelper (@ Nullable ContextSnapshotFactory factory ) {
552
+ this .snapshotFactory = (factory != null ? factory : ContextSnapshotFactory .builder ().build ());
553
+ }
538
554
539
555
@ SuppressWarnings ("ReactiveStreamsUnusedPublisher" )
540
- public static Object writeReactorContext (Object returnValue , ContextSnapshotFactory snapshotFactory ) {
556
+ public Object writeReactorContext (Object returnValue ) {
541
557
if (Mono .class .isAssignableFrom (returnValue .getClass ())) {
542
- ContextSnapshot snapshot = snapshotFactory .captureAll ();
558
+ ContextSnapshot snapshot = this . snapshotFactory .captureAll ();
543
559
return ((Mono <?>) returnValue ).contextWrite (snapshot ::updateContext );
544
560
}
545
561
else if (Flux .class .isAssignableFrom (returnValue .getClass ())) {
546
- ContextSnapshot snapshot = snapshotFactory .captureAll ();
562
+ ContextSnapshot snapshot = this . snapshotFactory .captureAll ();
547
563
return ((Flux <?>) returnValue ).contextWrite (snapshot ::updateContext );
548
564
}
549
565
else {
550
566
return returnValue ;
551
567
}
552
568
}
553
569
554
- public static TaskDecorator getTaskDecorator (ContextSnapshotFactory snapshotFactory ) {
555
- return new ContextPropagatingTaskDecorator (snapshotFactory );
570
+ public TaskDecorator getTaskDecorator () {
571
+ return new ContextPropagatingTaskDecorator (this . snapshotFactory );
556
572
}
557
573
}
558
574
0 commit comments