Public method to register MessagingAdviceBean
Closes gh-26636
This commit is contained in:
@@ -44,6 +44,7 @@ import org.springframework.messaging.Message;
|
|||||||
import org.springframework.messaging.handler.CompositeMessageCondition;
|
import org.springframework.messaging.handler.CompositeMessageCondition;
|
||||||
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
|
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
|
||||||
import org.springframework.messaging.handler.HandlerMethod;
|
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.MessageMapping;
|
||||||
import org.springframework.messaging.handler.annotation.support.AnnotationExceptionHandlerMethodResolver;
|
import org.springframework.messaging.handler.annotation.support.AnnotationExceptionHandlerMethodResolver;
|
||||||
import org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver;
|
import org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver;
|
||||||
@@ -189,6 +190,40 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
|
|||||||
this.valueResolver = 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
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
|
|||||||
Reference in New Issue
Block a user