类 AbstractMethodMessageHandler<T>
- java.lang.Object
- org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler<T>
- 类型参数:
T
- the type of the Object that contains information mapping aHandlerMethod
to incoming messages
- 所有已实现的接口:
Aware
,InitializingBean
,ApplicationContextAware
,MessageHandler
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.
- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev, Juergen Hoeller
构造器概要
构造器 构造器 说明 AbstractMethodMessageHandler()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.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()
Return the complete list of argument resolvers.List<HandlerMethodArgumentResolver>
getCustomArgumentResolvers()
Return the configured custom argument resolvers, if any.List<HandlerMethodReturnValueHandler>
getCustomReturnValueHandlers()
Return the configured custom return value handlers, if any.protected abstract String
getDestination(Message<?> message)
Collection<String>
getDestinationPrefixes()
Return the configured destination prefixes, if any.protected abstract Set<String>
getDirectLookupDestinations(T mapping)
Return destinations contained in the mapping that are not patterns and are therefore suitable for direct lookups.protected InvocableHandlerMethod
getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception)
Find an@MessageExceptionHandler
method for the given exception.protected Log
getHandlerMethodLogger()
Return a logger to set onInvocableHandlerMethod
.Map<T,HandlerMethod>
getHandlerMethods()
Return a map with all handler methods and their mappings.protected String
getLookupDestination(String destination)
Check whether the given destination (of an incoming message) matches to one of the configured destination prefixes and if so return the remaining portion of the destination after the matched prefix.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.protected Log
getReturnValueHandlerLogger()
Return a logger to set onHandlerMethodReturnValueHandlerComposite
.List<HandlerMethodReturnValueHandler>
getReturnValueHandlers()
Return the complete list of return value handlers.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 exception, Message<?> message)
protected void
registerExceptionHandlerAdvice(MessagingAdviceBean bean, AbstractExceptionHandlerMethodResolver resolver)
Subclasses can invoke this method to populate the MessagingAdviceBean cache (e.g. to support "global"@MessageExceptionHandler
).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 customHandlerMethodArgumentResolver
s that will be used after resolvers for supported argument type.void
setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> customReturnValueHandlers)
Set the list of customHandlerMethodReturnValueHandler
s that will be used after return value handlers for known types.void
setDestinationPrefixes(Collection<String> prefixes)
When this property is configured only messages to destinations matching one of the configured prefixes are eligible for handling.void
setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers)
Configure the complete list of supported return value types, effectively overriding the ones configured by default.String
toString()
构造器详细资料
AbstractMethodMessageHandler
public AbstractMethodMessageHandler()
方法详细资料
setDestinationPrefixes
public void setDestinationPrefixes(@Nullable Collection<String> prefixes)
When this property is configured only messages to destinations matching one of the configured prefixes are eligible for handling. When there is a match the prefix is removed and only the remaining part of the destination is used for method-mapping purposes.By default, no prefixes are configured in which case all messages are eligible for handling.
getDestinationPrefixes
public Collection<String> getDestinationPrefixes()
Return the configured destination prefixes, if any.
setCustomArgumentResolvers
public void setCustomArgumentResolvers(@Nullable List<HandlerMethodArgumentResolver> customArgumentResolvers)
Sets the list of customHandlerMethodArgumentResolver
s that will be used after resolvers for supported argument type.
getCustomArgumentResolvers
public List<HandlerMethodArgumentResolver> getCustomArgumentResolvers()
Return the configured custom argument resolvers, if any.
setCustomReturnValueHandlers
public void setCustomReturnValueHandlers(@Nullable List<HandlerMethodReturnValueHandler> customReturnValueHandlers)
Set the list of customHandlerMethodReturnValueHandler
s that will be used after return value handlers for known types.
getCustomReturnValueHandlers
public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()
Return the configured custom return value handlers, if any.
setArgumentResolvers
public void setArgumentResolvers(@Nullable 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 usesetCustomArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.
getArgumentResolvers
public List<HandlerMethodArgumentResolver> getArgumentResolvers()
Return the complete list of argument resolvers.
setReturnValueHandlers
public void setReturnValueHandlers(@Nullable 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 usesetCustomReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.
getReturnValueHandlers
public List<HandlerMethodReturnValueHandler> getReturnValueHandlers()
Return the complete list of return value handlers.
setApplicationContext
public void setApplicationContext(@Nullable ApplicationContext applicationContext)
从接口复制的说明: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 afterResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
andMessageSourceAware
, if applicable.- 指定者:
setApplicationContext
在接口中ApplicationContextAware
- 参数:
applicationContext
- the ApplicationContext object to be used by this object- 另请参阅:
BeanInitializationException
getApplicationContext
@Nullable public ApplicationContext getApplicationContext()
afterPropertiesSet
public void afterPropertiesSet()
从接口复制的说明:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- 指定者:
afterPropertiesSet
在接口中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 viasetArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.Subclasses should also take into account custom argument types configured via
setCustomArgumentResolvers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver>)
.
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 viasetReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.Subclasses should also take into account custom return value types configured via
setCustomReturnValueHandlers(java.util.List<org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler>)
.
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.- 参数:
handler
- the handler to check, either an instance of a Spring bean name
getMappingForMethod
@Nullable protected abstract T getMappingForMethod(Method method, Class<?> handlerType)
Provide the mapping for a handler method.- 参数:
method
- the method to provide a mapping forhandlerType
- the handler type, possibly a sub-type of the method's declaring class- 返回:
- 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.- 参数:
handler
- the bean name of the handler or the handler instancemethod
- the method to registermapping
- the mapping conditions associated with the handler method- 抛出:
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.
getReturnValueHandlerLogger
@Nullable protected Log getReturnValueHandlerLogger()
Return a logger to set onHandlerMethodReturnValueHandlerComposite
.- 从以下版本开始:
- 5.1
getHandlerMethodLogger
@Nullable protected Log getHandlerMethodLogger()
Return a logger to set onInvocableHandlerMethod
.- 从以下版本开始:
- 5.1
registerExceptionHandlerAdvice
protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean, AbstractExceptionHandlerMethodResolver resolver)
Subclasses can invoke this method to populate the MessagingAdviceBean cache (e.g. to support "global"@MessageExceptionHandler
).- 从以下版本开始:
- 4.2
getHandlerMethods
public Map<T,HandlerMethod> getHandlerMethods()
Return a map with all handler methods and their mappings.
handleMessage
public void handleMessage(Message<?> message) throws MessagingException
从接口复制的说明:MessageHandler
Handle the given message.- 指定者:
handleMessage
在接口中MessageHandler
- 参数:
message
- the message to be handled- 抛出:
MessagingException
- if the handler failed to process the message
getDestination
@Nullable protected abstract String getDestination(Message<?> message)
getLookupDestination
@Nullable protected String getLookupDestination(@Nullable String destination)
Check whether the given destination (of an incoming message) matches to one of the configured destination prefixes and if so return the remaining portion of the destination after the matched prefix.If there are no matching prefixes, return
null
.If there are no destination prefixes, return the destination as is.
handleMessageInternal
protected void handleMessageInternal(Message<?> message, String lookupDestination)
getMatchingMapping
@Nullable 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.- 参数:
mapping
- the mapping to get a match formessage
- the message being handled- 返回:
- the match or
null
if there is no match
handleNoMatch
protected void handleNoMatch(Set<T> ts, String lookupDestination, Message<?> message)
getMappingComparator
protected abstract Comparator<T> getMappingComparator(Message<?> message)
Return a comparator for sorting matching mappings. The returned comparator should sort 'better' matches higher.- 参数:
message
- the current Message- 返回:
- the comparator, never
null
handleMatch
protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookupDestination, Message<?> message)
processHandlerMethodException
protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception exception, Message<?> message)
getExceptionHandlerMethod
@Nullable protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception)
Find an@MessageExceptionHandler
method for the given exception. The default implementation searches methods in the class hierarchy of the HandlerMethod first and if not found, it continues searching for additional@MessageExceptionHandler
methods among the configured MessagingAdviceBean, if any.- 参数:
handlerMethod
- the method where the exception was raisedexception
- the raised exception- 返回:
- a method to handle the exception, or
null
- 从以下版本开始:
- 4.2
createExceptionHandlerMethodResolverFor
protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class<?> beanType)