Spring Framework

org.springframework.web.servlet.handler
Class AbstractHandlerMethodMapping<T>

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.handler.AbstractHandlerMapping
              extended by org.springframework.web.servlet.handler.AbstractHandlerMethodMapping<T>
Type Parameters:
T - The mapping for a HandlerMethod containing the conditions needed to match the handler method to incoming request.
All Implemented Interfaces:
Aware, InitializingBean, ApplicationContextAware, Ordered, ServletContextAware, HandlerMapping
Direct Known Subclasses:
RequestMappingInfoHandlerMapping

public abstract class AbstractHandlerMethodMapping<T>
extends AbstractHandlerMapping
implements InitializingBean

Abstract base class for HandlerMapping implementations that define a mapping between a request and a HandlerMethod.

For each registered handler method, a unique mapping is maintained with subclasses defining the details of the mapping type <T>.

Since:
3.1
Author:
Arjen Poutsma, Rossen Stoyanchev

Field Summary
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Fields inherited from interface org.springframework.web.servlet.HandlerMapping
BEST_MATCHING_PATTERN_ATTRIBUTE, INTROSPECT_TYPE_LEVEL_MAPPING, PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, URI_TEMPLATE_VARIABLES_ATTRIBUTE
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
AbstractHandlerMethodMapping()
           
 
Method Summary
 void afterPropertiesSet()
          Detects handler methods at initialization.
protected  void detectHandlerMethods(java.lang.Object handler)
          Look for handler methods in a handler.
protected  HandlerMethod getHandlerInternal(javax.servlet.http.HttpServletRequest request)
          Look up a handler method for the given request.
 java.util.Map<T,HandlerMethod> getHandlerMethods()
          Return a map with all handler methods and their mappings.
protected abstract  java.util.Comparator<T> getMappingComparator(javax.servlet.http.HttpServletRequest request)
          Return a comparator for sorting matching mappings.
protected abstract  T getMappingForMethod(java.lang.reflect.Method method, java.lang.Class<?> handlerType)
          Provide the mapping for a handler method.
protected abstract  java.util.Set<java.lang.String> getMappingPathPatterns(T mapping)
          Extract and return the URL paths contained in a mapping.
protected abstract  T getMatchingMapping(T mapping, javax.servlet.http.HttpServletRequest request)
          Check if a mapping matches the current request and return a (potentially new) mapping with conditions relevant to the current request.
protected  void handleMatch(T mapping, java.lang.String lookupPath, javax.servlet.http.HttpServletRequest request)
          Invoked when a matching mapping is found.
protected  HandlerMethod handleNoMatch(java.util.Set<T> mappings, java.lang.String lookupPath, javax.servlet.http.HttpServletRequest request)
          Invoked when no matching mapping is not found.
protected  void handlerMethodsInitialized(java.util.Map<T,HandlerMethod> handlerMethods)
          Invoked after all handler methods have been detected.
 void initApplicationContext()
          ApplicationContext initialization.
protected  void initHandlerMethods()
          Scan beans in the ApplicationContext, detect and register handler methods.
protected abstract  boolean isHandler(java.lang.Class<?> beanType)
          Whether the given type is a handler with handler methods.
protected  HandlerMethod lookupHandlerMethod(java.lang.String lookupPath, javax.servlet.http.HttpServletRequest request)
          Look up the best-matching handler method for the current request.
protected  void registerHandlerMethod(java.lang.Object handler, java.lang.reflect.Method method, T mapping)
          Register a handler method and its unique mapping.
 void setDetectHandlerMethodsInAncestorContexts(boolean detectHandlerMethodsInAncestorContexts)
          Whether to detect handler methods in beans in ancestor ApplicationContexts.
 
Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMapping
adaptInterceptor, detectMappedInterceptors, extendInterceptors, getAdaptedInterceptors, getDefaultHandler, getHandler, getHandlerExecutionChain, getMappedInterceptors, getOrder, getPathMatcher, getUrlPathHelper, initInterceptors, setAlwaysUseFullPath, setDefaultHandler, setInterceptors, setOrder, setPathMatcher, setUrlDecode, setUrlPathHelper
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractHandlerMethodMapping

public AbstractHandlerMethodMapping()
Method Detail

setDetectHandlerMethodsInAncestorContexts

public void setDetectHandlerMethodsInAncestorContexts(boolean detectHandlerMethodsInAncestorContexts)
Whether to detect handler methods in beans in ancestor ApplicationContexts.

Default is "false": Only beans in the current ApplicationContext are considered, i.e. only in the context that this HandlerMapping itself is defined in (typically the current DispatcherServlet's context).

Switch this flag on to detect handler beans in ancestor contexts (typically the Spring root WebApplicationContext) as well.


getHandlerMethods

public java.util.Map<T,HandlerMethod> getHandlerMethods()
Return a map with all handler methods and their mappings.


initApplicationContext

public void initApplicationContext()
                            throws ApplicationContextException
ApplicationContext initialization.

Overrides:
initApplicationContext in class AbstractHandlerMapping
Throws:
ApplicationContextException - in case of initialization errors
See Also:
AbstractHandlerMapping.extendInterceptors(java.util.List), AbstractHandlerMapping.initInterceptors()

afterPropertiesSet

public void afterPropertiesSet()
Detects handler methods at initialization.

Specified by:
afterPropertiesSet in interface InitializingBean

initHandlerMethods

protected void initHandlerMethods()
Scan beans in the ApplicationContext, detect and register handler methods.

See Also:
isHandler(Class), getMappingForMethod(Method, Class), handlerMethodsInitialized(Map)

isHandler

protected abstract boolean isHandler(java.lang.Class<?> beanType)
Whether the given type is a handler with handler methods.

Parameters:
beanType - the type of the bean being checked
Returns:
"true" if this a handler type, "false" otherwise.

handlerMethodsInitialized

protected void handlerMethodsInitialized(java.util.Map<T,HandlerMethod> handlerMethods)
Invoked after all handler methods have been detected.

Parameters:
handlerMethods - a read-only map with handler methods and mappings.

detectHandlerMethods

protected void detectHandlerMethods(java.lang.Object handler)
Look for handler methods in a handler.

Parameters:
handler - the bean name of a handler or a handler instance

getMappingForMethod

protected abstract T getMappingForMethod(java.lang.reflect.Method method,
                                         java.lang.Class<?> handlerType)
Provide the mapping for a handler method. A method for which no mapping can be provided is not 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(java.lang.Object handler,
                                     java.lang.reflect.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:
java.lang.IllegalStateException - if another method was already registered under the same mapping

getMappingPathPatterns

protected abstract java.util.Set<java.lang.String> getMappingPathPatterns(T mapping)
Extract and return the URL paths contained in a mapping.


getHandlerInternal

protected HandlerMethod getHandlerInternal(javax.servlet.http.HttpServletRequest request)
                                    throws java.lang.Exception
Look up a handler method for the given request.

Specified by:
getHandlerInternal in class AbstractHandlerMapping
Parameters:
request - current HTTP request
Returns:
the corresponding handler instance, or null if none found
Throws:
java.lang.Exception - if there is an internal error

lookupHandlerMethod

protected HandlerMethod lookupHandlerMethod(java.lang.String lookupPath,
                                            javax.servlet.http.HttpServletRequest request)
                                     throws java.lang.Exception
Look up the best-matching handler method for the current request. If multiple matches are found, the best match is selected.

Parameters:
lookupPath - mapping lookup path within the current servlet mapping
request - the current request
Returns:
the best-matching handler method, or null if no match
Throws:
java.lang.Exception
See Also:
handleMatch(Object, String, HttpServletRequest), handleNoMatch(Set, String, HttpServletRequest)

getMatchingMapping

protected abstract T getMatchingMapping(T mapping,
                                        javax.servlet.http.HttpServletRequest request)
Check if a mapping matches the current request and return a (potentially new) mapping with conditions relevant to the current request.

Parameters:
mapping - the mapping to get a match for
request - the current HTTP servlet request
Returns:
the match, or null if the mapping doesn't match

getMappingComparator

protected abstract java.util.Comparator<T> getMappingComparator(javax.servlet.http.HttpServletRequest request)
Return a comparator for sorting matching mappings. The returned comparator should sort 'better' matches higher.

Parameters:
request - the current request
Returns:
the comparator, never null

handleMatch

protected void handleMatch(T mapping,
                           java.lang.String lookupPath,
                           javax.servlet.http.HttpServletRequest request)
Invoked when a matching mapping is found.

Parameters:
mapping - the matching mapping
lookupPath - mapping lookup path within the current servlet mapping
request - the current request

handleNoMatch

protected HandlerMethod handleNoMatch(java.util.Set<T> mappings,
                                      java.lang.String lookupPath,
                                      javax.servlet.http.HttpServletRequest request)
                               throws java.lang.Exception
Invoked when no matching mapping is not found.

Parameters:
mappings - all registered mappings
lookupPath - mapping lookup path within the current servlet mapping
request - the current request
Throws:
javax.servlet.ServletException - in case of errors
java.lang.Exception

Spring Framework