|
44 | 44 | import org.springframework.messaging.handler.CompositeMessageCondition;
|
45 | 45 | import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
|
46 | 46 | import org.springframework.messaging.handler.HandlerMethod;
|
| 47 | +import org.springframework.messaging.handler.MessagingAdviceBean; |
47 | 48 | import org.springframework.messaging.handler.annotation.MessageMapping;
|
48 | 49 | import org.springframework.messaging.handler.annotation.support.AnnotationExceptionHandlerMethodResolver;
|
49 | 50 | import org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver;
|
@@ -189,6 +190,40 @@ public void setEmbeddedValueResolver(StringValueResolver resolver) {
|
189 | 190 | this.valueResolver = resolver;
|
190 | 191 | }
|
191 | 192 |
|
| 193 | + /** |
| 194 | + * Use this method to register a {@link MessagingAdviceBean} that may contain |
| 195 | + * globally applicable |
| 196 | + * {@link org.springframework.messaging.handler.annotation.MessageExceptionHandler @MessageExceptionHandler} |
| 197 | + * methods. |
| 198 | + * <p>Note: spring-messaging does not depend on spring-web and therefore it |
| 199 | + * is not possible to explicitly support the registration of a |
| 200 | + * {@code @ControllerAdvice} bean. You can use the following adapter code |
| 201 | + * to register {@code @ControllerAdvice} beans here: |
| 202 | + * <pre> |
| 203 | + * ControllerAdviceBean.findAnnotatedBeans(context).forEach(bean -> |
| 204 | + * messageHandler.registerMessagingAdvice(new ControllerAdviceWrapper(bean)); |
| 205 | + * |
| 206 | + * public class ControllerAdviceWrapper implements MessagingAdviceBean { |
| 207 | + * private final ControllerAdviceBean delegate; |
| 208 | + * // delegate all methods |
| 209 | + * } |
| 210 | + * </pre> |
| 211 | + * |
| 212 | + * @param bean the bean to check for {@code @MessageExceptionHandler} methods |
| 213 | + * @since 5.3.5 |
| 214 | + */ |
| 215 | + public void registerMessagingAdvice(MessagingAdviceBean bean) { |
| 216 | + Class<?> type = bean.getBeanType(); |
| 217 | + if (type != null) { |
| 218 | + AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(type); |
| 219 | + if (resolver.hasExceptionMappings()) { |
| 220 | + registerExceptionHandlerAdvice(bean, resolver); |
| 221 | + if (logger.isTraceEnabled()) { |
| 222 | + logger.trace("Detected @MessageExceptionHandler methods in " + bean); |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + } |
192 | 227 |
|
193 | 228 | @Override
|
194 | 229 | public void afterPropertiesSet() {
|
|
0 commit comments