概述
这是一个Spring MVC
助手类,用于集合应用所配置的HandlerMapping
(url pattern
和请求处理handler
之间的映射)表,用于获取针对某个请求的如下信息 :
getMatchableHandlerMapping(javax.servlet.http.HttpServletRequest)
寻找能处理指定请求的
HandlerMapping
,如果找不到,返回null
;如果找到的是一个MatchableHandlerMapping
,则返回;如果找得到当并不是MatchableHandlerMapping
,则抛出异常IllegalStateException
。getCorsConfiguration(javax.servlet.http.HttpServletRequest)
获取针对指定请求的
CORS
配置,封装为CorsConfiguration
。如果不存在相应配置,返回null
。
使用
- 作为
CorsConfigurationSource
使用
// 配置类 WebMvcConfigurationSupport
@Bean
@Lazy
public HandlerMappingIntrospector mvcHandlerMappingIntrospector() {
return new HandlerMappingIntrospector();
}
源代码
源代码版本
Spring Web MVC 5.1.5.RELEASE
package org.springframework.web.servlet.handler;
// 省略 imports
public class HandlerMappingIntrospector
implements CorsConfigurationSource, ApplicationContextAware, InitializingBean {
@Nullable
private ApplicationContext applicationContext;
@Nullable
private List<HandlerMapping> handlerMappings;
/**
* Constructor for use with ApplicationContextAware.
*/
public HandlerMappingIntrospector() {
}
/**
* Constructor that detects the configured {@code HandlerMapping}s in the
* given {@code ApplicationContext} or falls back on
* "DispatcherServlet.properties" like the {@code DispatcherServlet}.
* @deprecated as of 4.3.12, in favor of {@link #setApplicationContext}
*/
@Deprecated
public HandlerMappingIntrospector(ApplicationContext context) {
this.handlerMappings = initHandlerMappings(context);
}
/**
* Return the configured HandlerMapping's.
*/
public List<HandlerMapping> getHandlerMappings() {
return (this.handlerMappings != null ? this.handlerMappings : Collections.emptyList());
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
// InitializingBean 接口约定的方法,用于获取容器中定义的所有类型为 HandlerMapping 的 bean,
// 或者获取缺省定义的类型为 HandlerMapping 的 bean
@Override
public void afterPropertiesSet() {
if (this.handlerMappings == null) {
Assert.notNull(this.applicationContext, "No ApplicationContext");
this.handlerMappings = initHandlerMappings(this.applicationContext);
}
}
/**
* Find the {@link HandlerMapping} that would handle the given request and
* return it as a {@link MatchableHandlerMapping} that can be used to test
* request-matching criteria.
* <p>If the matching HandlerMapping is not an instance of
* {@link MatchableHandlerMapping}, an IllegalStateException is raised.
* @param request the current request
* @return the resolved matcher, or {@code null}
* @throws Exception if any of the HandlerMapping's raise an exception
*/
@Nullable
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
for (HandlerMapping handlerMapping : this.handlerMappings) {
Object handler = handlerMapping.getHandler(wrapper);
if (handler == null) {
continue;
}
if (handlerMapping instanceof MatchableHandlerMapping) {
return ((MatchableHandlerMapping) handlerMapping);
}
throw new IllegalStateException("HandlerMapping is not a MatchableHandlerMapping");
}
return null;
}
// 获取某个 request 请求对应的 CorsConfiguration
// 1. 从 handlerMappings 中找到该请求对应的 handler, 形式为 HandlerExecutionChain;
// 2. 检索 HandlerExecutionChain 中所有的
@Override
@Nullable
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
Assert.notNull(this.handlerMappings, "Handler mappings not initialized");
HttpServletRequest wrapper = new RequestAttributeChangeIgnoringWrapper(request);
for (HandlerMapping handlerMapping : this.handlerMappings) {
HandlerExecutionChain handler = null;
try {
handler = handlerMapping.getHandler(wrapper);
}
catch (Exception ex) {
// Ignore
}
if (handler == null) {
continue;
}
if (handler.getInterceptors() != null) {
for (HandlerInterceptor interceptor : handler.getInterceptors()) {
if (interceptor instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) interceptor).getCorsConfiguration(wrapper);
}
}
}
if (handler.getHandler() instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) handler.getHandler()).getCorsConfiguration(wrapper);
}
}
return null;
}
private static List<HandlerMapping> initHandlerMappings(ApplicationContext applicationContext) {
// 获取容器中所有类型为 HandlerMapping 的 bean
Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
applicationContext, HandlerMapping.class, true, false);
if (!beans.isEmpty()) {
List<HandlerMapping> mappings = new ArrayList<>(beans.values());
AnnotationAwareOrderComparator.sort(mappings);
return Collections.unmodifiableList(mappings);
}
// 容器中不存在类型为 HandlerMapping 的 bean,
// 此时使用 initFallback 创建 DispatcherServlet.properties 文件中
// 缺省定义的类型为 HandlerMapping 的 bean
return Collections.unmodifiableList(initFallback(applicationContext));
}
private static List<HandlerMapping> initFallback(ApplicationContext applicationContext) {
Properties props;
String path = "DispatcherServlet.properties";
try {
Resource resource = new ClassPathResource(path, DispatcherServlet.class);
props = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load '" + path + "': " + ex.getMessage());
}
String value = props.getProperty(HandlerMapping.class.getName());
String[] names = StringUtils.commaDelimitedListToStringArray(value);
List<HandlerMapping> result = new ArrayList<>(names.length);
for (String name : names) {
try {
Class<?> clazz = ClassUtils.forName(name, DispatcherServlet.class.getClassLoader());
// 使用容器创建 bean,该动作会使所创建的 bean 经历相应的生命周期处理,比如初始化,属性设置等等
Object mapping = applicationContext.getAutowireCapableBeanFactory().createBean(clazz);
result.add((HandlerMapping) mapping);
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException("Could not find default HandlerMapping [" + name + "]");
}
}
return result;
}
/**
* Request wrapper that ignores request attribute changes.
* 定义一个给当前 HandlerMappingIntrospector 使用的 HttpServletRequestWrapper,
* 其方法 setAttribute 实现为空,主要是用于避免属性修改,也就是保持对所访问的 request 不做修改
*/
private static class RequestAttributeChangeIgnoringWrapper extends HttpServletRequestWrapper {
public RequestAttributeChangeIgnoringWrapper(HttpServletRequest request) {
super(request);
}
@Override
public void setAttribute(String name, Object value) {
// Ignore attribute change...
}
}
}
转载:https://blog.csdn.net/andy_zhang2007/article/details/101024982
查看评论