diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java index 3b4bac9453bf..d8d3e55f19c3 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java @@ -44,6 +44,7 @@ import org.springframework.messaging.handler.CompositeMessageCondition; import org.springframework.messaging.handler.DestinationPatternsMessageCondition; import org.springframework.messaging.handler.HandlerMethod; +import org.springframework.messaging.handler.MessagingAdviceBean; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.support.AnnotationExceptionHandlerMethodResolver; import org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver; @@ -189,6 +190,40 @@ public void setEmbeddedValueResolver(StringValueResolver resolver) { this.valueResolver = resolver; } + /** + * Use this method to register a {@link MessagingAdviceBean} that may contain + * globally applicable + * {@link org.springframework.messaging.handler.annotation.MessageExceptionHandler @MessageExceptionHandler} + * methods. + *
Note: spring-messaging does not depend on spring-web and therefore it + * is not possible to explicitly support the registration of a + * {@code @ControllerAdvice} bean. You can use the following adapter code + * to register {@code @ControllerAdvice} beans here: + *
+ * ControllerAdviceBean.findAnnotatedBeans(context).forEach(bean -> + * messageHandler.registerMessagingAdvice(new ControllerAdviceWrapper(bean)); + * + * public class ControllerAdviceWrapper implements MessagingAdviceBean { + * private final ControllerAdviceBean delegate; + * // delegate all methods + * } + *+ * + * @param bean the bean to check for {@code @MessageExceptionHandler} methods + * @since 5.3.5 + */ + public void registerMessagingAdvice(MessagingAdviceBean bean) { + Class> type = bean.getBeanType(); + if (type != null) { + AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(type); + if (resolver.hasExceptionMappings()) { + registerExceptionHandlerAdvice(bean, resolver); + if (logger.isTraceEnabled()) { + logger.trace("Detected @MessageExceptionHandler methods in " + bean); + } + } + } + } @Override public void afterPropertiesSet() {