Skip to content

Commit

Permalink
Public method to register MessagingAdviceBean
Browse files Browse the repository at this point in the history
Closes gh-26636
  • Loading branch information
rstoyanchev committed Mar 15, 2021
1 parent c6b271f commit 6214ff1
Showing 1 changed file with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
* <p>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:
* <pre>
* ControllerAdviceBean.findAnnotatedBeans(context).forEach(bean ->
* messageHandler.registerMessagingAdvice(new ControllerAdviceWrapper(bean));
*
* public class ControllerAdviceWrapper implements MessagingAdviceBean {
* private final ControllerAdviceBean delegate;
* // delegate all methods
* }
* </pre>
*
* @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() {
Expand Down

0 comments on commit 6214ff1

Please sign in to comment.