T
- the mapping for a HandlerMethod
containing the conditions
needed to match the handler method to incoming request.public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMapping implements InitializingBean
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>
.
logger
BEST_MATCHING_HANDLER_ATTRIBUTE, BEST_MATCHING_PATTERN_ATTRIBUTE, INTROSPECT_TYPE_LEVEL_MAPPING, MATRIX_VARIABLES_ATTRIBUTE, PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, URI_TEMPLATE_VARIABLES_ATTRIBUTE
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
AbstractHandlerMethodMapping() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Detects handler methods at initialization.
|
protected HandlerMethod |
createHandlerMethod(Object handler,
Method method)
Create the HandlerMethod instance.
|
protected void |
detectHandlerMethods(Object handler)
Look for handler methods in the specified handler bean.
|
protected String[] |
getCandidateBeanNames()
Determine the names of candidate beans in the application context.
|
protected CorsConfiguration |
getCorsConfiguration(Object handler,
HttpServletRequest request)
Retrieve the CORS configuration for the given handler.
|
protected HandlerMethod |
getHandlerInternal(HttpServletRequest request)
Look up a handler method for the given request.
|
Map<T,HandlerMethod> |
getHandlerMethods()
Return a (read-only) map with all mappings and HandlerMethod's.
|
List<HandlerMethod> |
getHandlerMethodsForMappingName(String mappingName)
Return the handler methods for the given mapping name.
|
protected abstract Comparator<T> |
getMappingComparator(HttpServletRequest request)
Return a comparator for sorting matching mappings.
|
protected abstract T |
getMappingForMethod(Method method,
Class<?> handlerType)
Provide the mapping for a handler method.
|
protected abstract Set<String> |
getMappingPathPatterns(T mapping)
Extract and return the URL paths contained in a mapping.
|
protected abstract T |
getMatchingMapping(T mapping,
HttpServletRequest request)
Check if a mapping matches the current request and return a (potentially
new) mapping with conditions relevant to the current request.
|
HandlerMethodMappingNamingStrategy<T> |
getNamingStrategy()
Return the configured naming strategy or
null . |
protected void |
handleMatch(T mapping,
String lookupPath,
HttpServletRequest request)
Invoked when a matching mapping is found.
|
protected HandlerMethod |
handleNoMatch(Set<T> mappings,
String lookupPath,
HttpServletRequest request)
Invoked when no matching mapping is not found.
|
protected void |
handlerMethodsInitialized(Map<T,HandlerMethod> handlerMethods)
Invoked after all handler methods have been detected.
|
protected CorsConfiguration |
initCorsConfiguration(Object handler,
Method method,
T mapping)
Extract and return the CORS configuration for the mapping.
|
protected void |
initHandlerMethods()
Scan beans in the ApplicationContext, detect and register handler methods.
|
protected abstract boolean |
isHandler(Class<?> beanType)
Whether the given type is a handler with handler methods.
|
protected HandlerMethod |
lookupHandlerMethod(String lookupPath,
HttpServletRequest request)
Look up the best-matching handler method for the current request.
|
protected void |
processCandidateBean(String beanName)
Determine the type of the specified candidate bean and call
detectHandlerMethods(java.lang.Object) if identified as a handler type. |
protected void |
registerHandlerMethod(Object handler,
Method method,
T mapping)
Register a handler method and its unique mapping.
|
void |
registerMapping(T mapping,
Object handler,
Method method)
Register the given mapping.
|
void |
setDetectHandlerMethodsInAncestorContexts(boolean detectHandlerMethodsInAncestorContexts)
Whether to detect handler methods in beans in ancestor ApplicationContexts.
|
void |
setHandlerMethodMappingNamingStrategy(HandlerMethodMappingNamingStrategy<T> namingStrategy)
Configure the naming strategy to use for assigning a default name to every
mapped handler method.
|
void |
unregisterMapping(T mapping)
Un-register the given mapping.
|
adaptInterceptor, detectMappedInterceptors, extendInterceptors, formatMappingName, getAdaptedInterceptors, getCorsConfigurations, getCorsHandlerExecutionChain, getCorsProcessor, getDefaultHandler, getHandler, getHandlerExecutionChain, getMappedInterceptors, getOrder, getPathMatcher, getUrlPathHelper, initApplicationContext, initInterceptors, setAlwaysUseFullPath, setBeanName, setCorsConfigurations, setCorsConfigurationSource, setCorsProcessor, setDefaultHandler, setInterceptors, setOrder, setPathMatcher, setRemoveSemicolonContent, setUrlDecode, setUrlPathHelper
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext
public void setDetectHandlerMethodsInAncestorContexts(boolean detectHandlerMethodsInAncestorContexts)
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.
getCandidateBeanNames()
public void setHandlerMethodMappingNamingStrategy(HandlerMethodMappingNamingStrategy<T> namingStrategy)
The default naming strategy is based on the capital letters of the class name followed by "#" and then the method name, e.g. "TC#getFoo" for a class named TestController with method getFoo.
@Nullable public HandlerMethodMappingNamingStrategy<T> getNamingStrategy()
null
.public Map<T,HandlerMethod> getHandlerMethods()
@Nullable public List<HandlerMethod> getHandlerMethodsForMappingName(String mappingName)
mappingName
- the mapping namenull
; the returned
list will never be modified and is safe to iterate.setHandlerMethodMappingNamingStrategy(org.springframework.web.servlet.handler.HandlerMethodMappingNamingStrategy<T>)
public void registerMapping(T mapping, Object handler, Method method)
This method may be invoked at runtime after initialization has completed.
mapping
- the mapping for the handler methodhandler
- the handlermethod
- the methodpublic void unregisterMapping(T mapping)
This method may be invoked at runtime after initialization has completed.
mapping
- the mapping to unregisterpublic void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
initHandlerMethods()
protected void initHandlerMethods()
protected String[] getCandidateBeanNames()
protected void processCandidateBean(String beanName)
detectHandlerMethods(java.lang.Object)
if identified as a handler type.
This implementation avoids bean creation through checking
BeanFactory.getType(java.lang.String)
and calling detectHandlerMethods(java.lang.Object)
with the bean name.
beanName
- the name of the candidate beanisHandler(java.lang.Class<?>)
,
detectHandlerMethods(java.lang.Object)
protected void detectHandlerMethods(Object handler)
handler
- either a bean name or an actual handler instancegetMappingForMethod(java.lang.reflect.Method, java.lang.Class<?>)
protected void registerHandlerMethod(Object handler, Method method, T mapping)
handler
- the bean name of the handler or the handler instancemethod
- the method to registermapping
- the mapping conditions associated with the handler methodIllegalStateException
- if another method was already registered
under the same mappingprotected HandlerMethod createHandlerMethod(Object handler, Method method)
handler
- either a bean name or an actual handler instancemethod
- the target method@Nullable protected CorsConfiguration initCorsConfiguration(Object handler, Method method, T mapping)
protected void handlerMethodsInitialized(Map<T,HandlerMethod> handlerMethods)
handlerMethods
- a read-only map with handler methods and mappings.protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception
getHandlerInternal
in class AbstractHandlerMapping
request
- current HTTP requestnull
if none foundException
- if there is an internal error@Nullable protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception
lookupPath
- mapping lookup path within the current servlet mappingrequest
- the current requestnull
if no matchException
handleMatch(Object, String, HttpServletRequest)
,
handleNoMatch(Set, String, HttpServletRequest)
protected void handleMatch(T mapping, String lookupPath, HttpServletRequest request)
mapping
- the matching mappinglookupPath
- mapping lookup path within the current servlet mappingrequest
- the current request@Nullable protected HandlerMethod handleNoMatch(Set<T> mappings, String lookupPath, HttpServletRequest request) throws Exception
mappings
- all registered mappingslookupPath
- mapping lookup path within the current servlet mappingrequest
- the current requestServletException
- in case of errorsException
protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request)
AbstractHandlerMapping
getCorsConfiguration
in class AbstractHandlerMapping
handler
- the handler to check (never null
).request
- the current request.null
if noneprotected abstract boolean isHandler(Class<?> beanType)
beanType
- the type of the bean being checked@Nullable protected abstract T getMappingForMethod(Method method, Class<?> handlerType)
method
- the method to provide a mapping forhandlerType
- the handler type, possibly a sub-type of the method's
declaring classnull
if the method is not mappedprotected abstract Set<String> getMappingPathPatterns(T mapping)
@Nullable protected abstract T getMatchingMapping(T mapping, HttpServletRequest request)
mapping
- the mapping to get a match forrequest
- the current HTTP servlet requestnull
if the mapping doesn't matchprotected abstract Comparator<T> getMappingComparator(HttpServletRequest request)
request
- the current requestnull
)