Skip to content

Commit 772087f

Browse files
committed
Refactor RSocket handler selection
1. Consolidate config options for handler detection in the base class AbstractMethodMessageHandler with sub-classes like RSocketMessageHandler now only setting the handler predicate by default (e.g. @controller). 2. Remove autoDetection flag in favor of just having the mutually exclusive handler Predicate<Object> vs manually registered List<Object>. Or if both are desired for some reason, then manually register first, and set the predicate second.
1 parent 7d68a65 commit 772087f

File tree

3 files changed

+52
-77
lines changed

3 files changed

+52
-77
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@
8383
public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<CompositeMessageCondition>
8484
implements EmbeddedValueResolverAware {
8585

86-
@Nullable
87-
private Predicate<Class<?>> handlerPredicate =
88-
beanType -> AnnotatedElementUtils.hasAnnotation(beanType, Controller.class);
89-
9086
private final List<Decoder<?>> decoders = new ArrayList<>();
9187

9288
@Nullable
@@ -104,59 +100,10 @@ public MessageMappingMessageHandler() {
104100
AntPathMatcher pathMatcher = new AntPathMatcher();
105101
pathMatcher.setPathSeparator(".");
106102
this.routeMatcher = new SimpleRouteMatcher(pathMatcher);
103+
setHandlerPredicate(type -> AnnotatedElementUtils.hasAnnotation(type, Controller.class));
107104
}
108105

109106

110-
/**
111-
* Manually configure handlers to check for {@code @MessageMapping} methods.
112-
* <p><strong>Note:</strong> the given handlers are not required to be
113-
* annotated with {@code @Controller}. Consider also using
114-
* {@link #setAutoDetectDisabled()} if the intent is to use these handlers
115-
* instead of, and not in addition to {@code @Controller} classes. Or
116-
* alternatively use {@link #setHandlerPredicate(Predicate)} to select a
117-
* different set of beans based on a different criteria.
118-
* @param handlers the handlers to register
119-
* @see #setAutoDetectDisabled()
120-
* @see #setHandlerPredicate(Predicate)
121-
*/
122-
public void setHandlers(List<Object> handlers) {
123-
for (Object handler : handlers) {
124-
detectHandlerMethods(handler);
125-
}
126-
// Disable auto-detection..
127-
this.handlerPredicate = null;
128-
}
129-
130-
/**
131-
* Configure the predicate to use for selecting which Spring beans to check
132-
* for {@code @MessageMapping} methods. When set to {@code null},
133-
* auto-detection is turned off which is what
134-
* {@link #setAutoDetectDisabled()} does internally.
135-
* <p>The predicate used by default selects {@code @Controller} classes.
136-
* @see #setHandlers(List)
137-
* @see #setAutoDetectDisabled()
138-
*/
139-
public void setHandlerPredicate(@Nullable Predicate<Class<?>> handlerPredicate) {
140-
this.handlerPredicate = handlerPredicate;
141-
}
142-
143-
/**
144-
* Return the {@link #setHandlerPredicate configured} handler predicate.
145-
*/
146-
@Nullable
147-
public Predicate<Class<?>> getHandlerPredicate() {
148-
return this.handlerPredicate;
149-
}
150-
151-
/**
152-
* Disable auto-detection of {@code @MessageMapping} methods, e.g. in
153-
* {@code @Controller}s, by setting {@link #setHandlerPredicate(Predicate)
154-
* setHandlerPredicate(null)}.
155-
*/
156-
public void setAutoDetectDisabled() {
157-
this.handlerPredicate = null;
158-
}
159-
160107
/**
161108
* Configure the decoders to use for incoming payloads.
162109
*/
@@ -264,11 +211,6 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler
264211
return Collections.emptyList();
265212
}
266213

267-
@Override
268-
protected Predicate<Class<?>> initHandlerPredicate() {
269-
return this.handlerPredicate;
270-
}
271-
272214

273215
@Override
274216
protected CompositeMessageCondition getMappingForMethod(Method method, Class<?> handlerType) {

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractMethodMessageHandler.java

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ public abstract class AbstractMethodMessageHandler<T>
8686
protected final Log logger = LogFactory.getLog(getClass());
8787

8888

89+
@Nullable
90+
private Predicate<Class<?>> handlerPredicate;
91+
92+
@Nullable
93+
List<Object> handlers;
94+
8995
private ArgumentResolverConfigurer argumentResolverConfigurer = new ArgumentResolverConfigurer();
9096

9197
private ReturnValueHandlerConfigurer returnValueHandlerConfigurer = new ReturnValueHandlerConfigurer();
@@ -103,6 +109,38 @@ public abstract class AbstractMethodMessageHandler<T>
103109
private final MultiValueMap<String, T> destinationLookup = new LinkedMultiValueMap<>(64);
104110

105111

112+
/**
113+
* Configure a predicate for selecting which Spring beans to check for the
114+
* presence of message handler methods.
115+
* <p>This is not set by default. However sub-classes may initialize it to
116+
* some default strategy (e.g. {@code @Controller} classes).
117+
* @see #setHandlers(List)
118+
*/
119+
public void setHandlerPredicate(@Nullable Predicate<Class<?>> handlerPredicate) {
120+
this.handlerPredicate = handlerPredicate;
121+
}
122+
123+
/**
124+
* Return the {@link #setHandlerPredicate configured} handler predicate.
125+
*/
126+
@Nullable
127+
public Predicate<Class<?>> getHandlerPredicate() {
128+
return this.handlerPredicate;
129+
}
130+
131+
/**
132+
* Manually configure the handlers to check for the presence of message
133+
* handling methods, which also disables auto-detection via a
134+
* {@link #setHandlerPredicate(Predicate) handlerPredicate}. If you do not
135+
* want to disable auto-detection, then call this method first, and then set
136+
* the handler predicate.
137+
* @param handlers the handlers to check
138+
*/
139+
public void setHandlers(List<Object> handlers) {
140+
this.handlers = handlers;
141+
this.handlerPredicate = null;
142+
}
143+
106144
/**
107145
* Configure custom resolvers for handler method arguments.
108146
*/
@@ -233,9 +271,14 @@ private void initHandlerMethods() {
233271
logger.warn("No ApplicationContext available for detecting beans with message handling methods.");
234272
return;
235273
}
236-
Predicate<Class<?>> handlerPredicate = initHandlerPredicate();
237-
if (handlerPredicate == null) {
238-
logger.warn("[" + getBeanName() + "] No auto-detection of handler methods (e.g. in @Controller).");
274+
if (this.handlers != null) {
275+
for (Object handler : this.handlers) {
276+
detectHandlerMethods(handler);
277+
}
278+
}
279+
Predicate<Class<?>> predicate = this.handlerPredicate;
280+
if (predicate == null) {
281+
logger.warn("[" + getBeanName() + "] Auto-detection of message handling methods is off.");
239282
return;
240283
}
241284
for (String beanName : this.applicationContext.getBeanNamesForType(Object.class)) {
@@ -250,21 +293,13 @@ private void initHandlerMethods() {
250293
logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex);
251294
}
252295
}
253-
if (beanType != null && handlerPredicate.test(beanType)) {
296+
if (beanType != null && predicate.test(beanType)) {
254297
detectHandlerMethods(beanName);
255298
}
256299
}
257300
}
258301
}
259302

260-
/**
261-
* Return the predicate to use to check whether a given Spring bean should
262-
* be introspected for message handling methods. If {@code null} is
263-
* returned, auto-detection is effectively disabled.
264-
*/
265-
@Nullable
266-
protected abstract Predicate<Class<?>> initHandlerPredicate();
267-
268303
/**
269304
* Detect if the given handler has any methods that can handle messages and if
270305
* so register it with the extracted mapping information.

spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/reactive/MethodMessageHandlerTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Map;
2525
import java.util.Set;
2626
import java.util.function.Consumer;
27-
import java.util.function.Predicate;
2827

2928
import org.junit.Test;
3029
import org.reactivestreams.Publisher;
@@ -203,6 +202,10 @@ private static class TestMethodMessageHandler extends AbstractMethodMessageHandl
203202
private PathMatcher pathMatcher = new AntPathMatcher();
204203

205204

205+
public TestMethodMessageHandler() {
206+
setHandlerPredicate(handlerType -> handlerType.getName().endsWith("Controller"));
207+
}
208+
206209
@Override
207210
protected List<? extends HandlerMethodArgumentResolver> initArgumentResolvers() {
208211
return Collections.emptyList();
@@ -213,11 +216,6 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler
213216
return Collections.singletonList(this.returnValueHandler);
214217
}
215218

216-
@Override
217-
protected Predicate<Class<?>> initHandlerPredicate() {
218-
return handlerType -> handlerType.getName().endsWith("Controller");
219-
}
220-
221219
@Nullable
222220
public Object getLastReturnValue() {
223221
return this.returnValueHandler.getLastReturnValue();

0 commit comments

Comments
 (0)