org.springframework.messaging.handler.invocation

## Class AbstractMethodMessageHandler<T>

- java.lang.Object
- - org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler<T>

- Type Parameters:
    `T` - the type of the Object that contains information mapping a `HandlerMethod` to incoming messages
  All Implemented Interfaces:
    Aware, InitializingBean, ApplicationContextAware, MessageHandler
  Direct Known Subclasses:
    SimpAnnotationMethodMessageHandler

      public abstract class AbstractMethodMessageHandler<T>
      extends Object
      implements MessageHandler, ApplicationContextAware, InitializingBean

  Abstract base class for HandlerMethod-based message handling. Provides most of the logic required to discover handler methods at startup, find a matching handler method at runtime for a given message and invoke it.

  Also supports discovering and invoking exception handling methods to process exceptions raised during message handling.

  Since:
    4.0
  Author:
    Rossen Stoyanchev

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Log` | `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractMethodMessageHandler()`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `afterPropertiesSet()` Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
    `protected abstract AbstractExceptionHandlerMethodResolver` | `createExceptionHandlerMethodResolverFor(Class<?> beanType)`
    `protected HandlerMethod` | `createHandlerMethod(Object handler, Method method)` Create a HandlerMethod instance from an Object handler that is either a handler instance or a String-based bean name.
    `protected void` | `detectHandlerMethods(Object handler)` Detect if the given handler has any methods that can handle messages and if so register it with the extracted mapping information.
    `ApplicationContext` | `getApplicationContext()`
    `List<HandlerMethodArgumentResolver>` | `getArgumentResolvers()`
    `List<HandlerMethodArgumentResolver>` | `getCustomArgumentResolvers()`
    `List<HandlerMethodReturnValueHandler>` | `getCustomReturnValueHandlers()`
    `protected abstract String` | `getDestination(Message<?> message)`
    `Collection<String>` | `getDestinationPrefixes()`
    `protected abstract Set<String>` | `getDirectLookupDestinations(T mapping)` Return destinations contained in the mapping that are not patterns and are therefore suitable for direct lookups.
    `Map<T,HandlerMethod>` | `getHandlerMethods()` Return a map with all handler methods and their mappings.
    `protected String` | `getLookupDestination(String destination)` Find if the given destination matches any of the configured allowed destination prefixes and if a match is found return the destination with the prefix removed.
    `protected abstract Comparator<T>` | `getMappingComparator(Message<?> message)` Return a comparator for sorting matching mappings.
    `protected abstract T` | `getMappingForMethod(Method method, Class<?> handlerType)` Provide the mapping for a handler method.
    `protected abstract T` | `getMatchingMapping(T mapping, Message<?> message)` Check if a mapping matches the current message and return a possibly new mapping with conditions relevant to the current request.
    `List<HandlerMethodReturnValueHandler>` | `getReturnValueHandlers()`
    `protected void` | `handleMatch(T mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message)`
    `void` | `handleMessage(Message<?> message)` Handle the given message.
    `protected void` | `handleMessageInternal(Message<?> message, String lookupDestination)`
    `protected void` | `handleNoMatch(Set<T> ts, String lookupDestination, Message<?> message)`
    `protected abstract List<? extends HandlerMethodArgumentResolver>` | `initArgumentResolvers()` Return the list of argument resolvers to use.
    `protected abstract List<? extends HandlerMethodReturnValueHandler>` | `initReturnValueHandlers()` Return the list of return value handlers to use.
    `protected abstract boolean` | `isHandler(Class<?> beanType)` Whether the given bean type should be introspected for messaging handling methods.
    `protected void` | `processHandlerMethodException(HandlerMethod handlerMethod, Exception ex, Message<?> message)`
    `protected void` | `registerHandlerMethod(Object handler, Method method, T mapping)` Register a handler method and its unique mapping.
    `void` | `setApplicationContext(ApplicationContext applicationContext)` Set the ApplicationContext that this object runs in.
    `void` | `setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)` Configure the complete list of supported argument types effectively overriding the ones configured by default.
    `void` | `setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> customArgumentResolvers)` Sets the list of custom `HandlerMethodArgumentResolver`s that will be used after resolvers for supported argument type.
    `void` | `setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> customReturnValueHandlers)` Set the list of custom `HandlerMethodReturnValueHandler`s that will be used after return value handlers for known types.
    `void` | `setDestinationPrefixes(Collection<String> prefixes)` Configure one or more prefixes to match to the destinations of handled messages.
    `void` | `setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers)` Configure the complete list of supported return value types effectively overriding the ones configured by default.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### logger

          protected final Log logger

  - ### Constructor Detail

    - #### AbstractMethodMessageHandler

          public AbstractMethodMessageHandler()

  - ### Method Detail

    - #### setDestinationPrefixes

          public void setDestinationPrefixes(Collection<String> prefixes)

      Configure one or more prefixes to match to the destinations of handled messages. Messages whose destination does not start with one of the configured prefixes are ignored. When a destination matches one of the configured prefixes, the matching part is removed from destination before performing a lookup for a matching message handling method. Prefixes without a trailing slash will have one appended automatically.

      By default the list of prefixes is empty in which case all destinations match.

    - #### getDestinationPrefixes

          public Collection<String> getDestinationPrefixes()

    - #### setCustomArgumentResolvers

          public void setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> customArgumentResolvers)

      Sets the list of custom `HandlerMethodArgumentResolver`s that will be used after resolvers for supported argument type.
      Parameters:
        `customArgumentResolvers` - the list of resolvers; never `null`.

    - #### getCustomArgumentResolvers

          public List<HandlerMethodArgumentResolver> getCustomArgumentResolvers()

    - #### setCustomReturnValueHandlers

          public void setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> customReturnValueHandlers)

      Set the list of custom `HandlerMethodReturnValueHandler`s that will be used after return value handlers for known types.
      Parameters:
        `customReturnValueHandlers` - the list of custom return value handlers, never `null`.

    - #### getCustomReturnValueHandlers

          public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()

    - #### setArgumentResolvers

          public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)

      Configure the complete list of supported argument types effectively overriding the ones configured by default. This is an advanced option. For most use cases it should be sufficient to use `setCustomArgumentResolvers(java.util.List)`.

    - #### getArgumentResolvers

          public List<HandlerMethodArgumentResolver> getArgumentResolvers()

    - #### setReturnValueHandlers

          public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers)

      Configure the complete list of supported return value types effectively overriding the ones configured by default. This is an advanced option. For most use cases it should be sufficient to use `setCustomReturnValueHandlers(java.util.List)`

    - #### getReturnValueHandlers

          public List<HandlerMethodReturnValueHandler> getReturnValueHandlers()

    - #### getHandlerMethods

          public Map<T,HandlerMethod> getHandlerMethods()

      Return a map with all handler methods and their mappings.

    - #### setApplicationContext

          public void setApplicationContext(ApplicationContext applicationContext)
                                     throws BeansException

      Description copied from interface: `ApplicationContextAware`
      Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

      Invoked after population of normal bean properties but before an init callback such as `InitializingBean.afterPropertiesSet()` or a custom init-method. Invoked after `ResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)`, `ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)` and `MessageSourceAware`, if applicable.

      Specified by:
        `setApplicationContext` in interface `ApplicationContextAware`
      Parameters:
        `applicationContext` - the ApplicationContext object to be used by this object
      Throws:
        `ApplicationContextException` - in case of context initialization errors
        `BeansException` - if thrown by application context methods
      See Also:
        `BeanInitializationException`

    - #### getApplicationContext

          public ApplicationContext getApplicationContext()

    - #### afterPropertiesSet

          public void afterPropertiesSet()

      Description copied from interface: `InitializingBean`
      Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

      This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

      Specified by:
        `afterPropertiesSet` in interface `InitializingBean`

    - #### initArgumentResolvers

          protected abstract List<? extends HandlerMethodArgumentResolver> initArgumentResolvers()

      Return the list of argument resolvers to use. Invoked only if the resolvers have not already been set via `setArgumentResolvers(java.util.List)`.

      Sub-classes should also take into account custom argument types configured via `setCustomArgumentResolvers(java.util.List)`.

    - #### initReturnValueHandlers

          protected abstract List<? extends HandlerMethodReturnValueHandler> initReturnValueHandlers()

      Return the list of return value handlers to use. Invoked only if the return value handlers have not already been set via `setReturnValueHandlers(java.util.List)`.

      Sub-classes should also take into account custom return value types configured via `setCustomReturnValueHandlers(java.util.List)`.

    - #### isHandler

          protected abstract boolean isHandler(Class<?> beanType)

      Whether the given bean type should be introspected for messaging handling methods.

    - #### detectHandlerMethods

          protected final void detectHandlerMethods(Object handler)

      Detect if the given handler has any methods that can handle messages and if so register it with the extracted mapping information.
      Parameters:
        `handler` - the handler to check, either an instance of a Spring bean name

    - #### getMappingForMethod

          protected abstract T getMappingForMethod(Method method,
                                                   Class<?> handlerType)

      Provide the mapping for a handler method.
      Parameters:
        `method` - the method to provide a mapping for
        `handlerType` - the handler type, possibly a sub-type of the method's declaring class
      Returns:
        the mapping, or `null` if the method is not mapped

    - #### registerHandlerMethod

          protected void registerHandlerMethod(Object handler,
                                               Method method,
                                               T mapping)

      Register a handler method and its unique mapping.
      Parameters:
        `handler` - the bean name of the handler or the handler instance
        `method` - the method to register
        `mapping` - the mapping conditions associated with the handler method
      Throws:
        `IllegalStateException` - if another method was already registered under the same mapping

    - #### createHandlerMethod

          protected HandlerMethod createHandlerMethod(Object handler,
                                                      Method method)

      Create a HandlerMethod instance from an Object handler that is either a handler instance or a String-based bean name.

    - #### getDirectLookupDestinations

          protected abstract Set<String> getDirectLookupDestinations(T mapping)

      Return destinations contained in the mapping that are not patterns and are therefore suitable for direct lookups.

    - #### handleMessage

          public void handleMessage(Message<?> message)
                             throws MessagingException

      Description copied from interface: `MessageHandler`
      Handle the given message.
      Specified by:
        `handleMessage` in interface `MessageHandler`
      Parameters:
        `message` - the message to be handled
      Throws:
        `MessagingException`

    - #### getDestination

          protected abstract String getDestination(Message<?> message)

    - #### getLookupDestination

          protected String getLookupDestination(String destination)

      Find if the given destination matches any of the configured allowed destination prefixes and if a match is found return the destination with the prefix removed.

      If no destination prefixes are configured, the destination is returned as is.

      Returns:
        the destination to use to find matching message handling methods or `null` if the destination does not match

    - #### handleMessageInternal

          protected void handleMessageInternal(Message<?> message,
                                               String lookupDestination)

    - #### getMatchingMapping

          protected abstract T getMatchingMapping(T mapping,
                                                  Message<?> message)

      Check if a mapping matches the current message and return a possibly new mapping with conditions relevant to the current request.
      Parameters:
        `mapping` - the mapping to get a match for
        `message` - the message being handled
      Returns:
        the match or `null` if there is no match

    - #### getMappingComparator

          protected abstract Comparator<T> getMappingComparator(Message<?> message)

      Return a comparator for sorting matching mappings. The returned comparator should sort 'better' matches higher.
      Parameters:
        `message` - the current Message
      Returns:
        the comparator, never `null`

    - #### handleMatch

          protected void handleMatch(T mapping,
                                     HandlerMethod handlerMethod,
                                     String lookupDestination,
                                     Message<?> message)

    - #### processHandlerMethodException

          protected void processHandlerMethodException(HandlerMethod handlerMethod,
                                                       Exception ex,
                                                       Message<?> message)

    - #### createExceptionHandlerMethodResolverFor

          protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class<?> beanType)

    - #### handleNoMatch

          protected void handleNoMatch(Set<T> ts,
                                       String lookupDestination,
                                       Message<?> message)