Spring 系列
- IoC 容器
- AOP
- SpringMVC
- Spring 事务
- Spring 源码故事(瞎编版)
- Spring 整体脉络
- Spring 类解析
- Spring 自定义标签解析
- Spring Scan 包扫描
- Spring 注解工具类
- Spring 别名注册
- Spring 标签解析类
- Spring ApplicationListener
- Spring messageSource
- Spring 自定义属性解析器
- Spring 排序工具
- Spring-import 注解
- Spring-定时任务
- Spring StopWatch
- Spring 元数据
- Spring 条件接口
- Spring MultiValueMap
- Spring MethodOverride
- Spring BeanDefinitionReaderUtils
- Spring PropertyPlaceholderHelper
- Spring-AnnotationFormatterFactory
- Spring-Formatter
- Spring-Parser
- Spring-Printer
- Spring5 新特性
- Spring RMI
- Spring Message
- SpringBoot
- SpringBootBatch
- Spring Cloud
- SpringSecurity
MyBatis
- 基础支持层
- 核心处理层
- 类解析
Netty
- 网络 IO 技术基础
- JDK1.8 NIO 包 核心组件源码剖析
- Netty 粘拆包及解决方案
- Netty 多协议开发
- 基于 Netty 开发服务端及客户端
- Netty 主要组件的源码分析
- Netty 高级特性
- Netty 技术细节源码分析
Dubbo
- 架构设计
- SPI 机制
- 注册中心
- 远程通信
- RPC
- 集群
Tomcat
- Servlet 与 Servlet 容器
- Web 容器
Redis
Nacos
Sentinel
RocketMQ
- RocketMQ NameServer 与 Broker 的通信
- RocketMQ 生产者启动流程
- RocketMQ 消息发送流程
- RocketMQ 消息发送存储流程
- RocketMQ MappedFile 内存映射文件详解
- RocketMQ ConsumeQueue 详解
- RocketMQ CommitLog 详解
- RocketMQ IndexFile 详解
- RocketMQ 消费者启动流程
- RocketMQ 消息拉取流程
- RocketMQ Broker 处理拉取消息请求流程
- RocketMQ 消息消费流程
番外篇(JDK 1.8)
- 基础类库
- 集合
- 并发编程
学习心得
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
Spring mvc MappingRegistry
MappingRegistry
- Author: HuiFer
- 源码阅读仓库: SourceHot-spring
源码路径:
org.springframework.jms.annotation.EnableJms
类全路径
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry
基本属性
class MappingRegistry { /** * key:mapping * value: mapping registration */ private final Map<T, MappingRegistration<T>> registry = new HashMap<>(); /** * key: mapping * value: handlerMethod */ private final Map<T, HandlerMethod> mappingLookup = new LinkedHashMap<>(); /** * key: url * value: list mapping */ private final MultiValueMap<String, T> urlLookup = new LinkedMultiValueMap<>(); /** * key: name * value: handler method */ private final Map<String, List<HandlerMethod>> nameLookup = new ConcurrentHashMap<>(); /** * key:handler method * value: 跨域配置 */ private final Map<HandlerMethod, CorsConfiguration> corsLookup = new ConcurrentHashMap<>(); /** * 读写锁 */ private final ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock(); }
写一个简单的 controller 来进行解析
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/do")
public Object go() {
return "fff";
}
}
前置链路追踪
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#registerHandlerMethod
protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) { super.registerHandlerMethod(handler, method, mapping); this.updateConsumesCondition(mapping, method); }
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#registerHandlerMethod
protected void registerHandlerMethod(Object handler, Method method, T mapping) { this.mappingRegistry.register(mapping, handler, method); }
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#register
本文重点的方法
先将对象截图出来方便后续理解
createHandlerMethod
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#createHandlerMethod
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
// 是否是字符串
if (handler instanceof String) {
// 创建对象
return new HandlerMethod((String) handler,
obtainApplicationContext().getAutowireCapableBeanFactory(), method);
}
return new HandlerMethod(handler, method);
}
HandlerMethod 构造函数
public HandlerMethod(String beanName, BeanFactory beanFactory, Method method){} public HandlerMethod(Object bean, Method method) {}
HandlerMethod
- 成员变量
public class HandlerMethod {
/** Logger that is available to subclasses. */
protected final Log logger = LogFactory.getLog(getClass());
/**
* beanName 或者 bean 实例
*/
private final Object bean;
/**
* 上下文
*/
@Nullable
private final BeanFactory beanFactory;
/**
* bean 类型
*/
private final Class<?> beanType;
/**
* 处理方法
*/
private final Method method;
private final Method bridgedMethod;
/**
* 方法参数
*/
private final MethodParameter[] parameters;
}
validateMethodMapping
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#validateMethodMapping
HandlerMethod 进行验证
private void validateMethodMapping(HandlerMethod handlerMethod, T mapping) {
// Assert that the supplied mapping is unique.
// 从缓存中获取
HandlerMethod existingHandlerMethod = this.mappingLookup.get(mapping);
// 是否为空 , 是否相同
if (existingHandlerMethod != null && !existingHandlerMethod.equals(handlerMethod)) {
throw new IllegalStateException(
"Ambiguous mapping. Cannot map '" + handlerMethod.getBean() + "' method \n" +
handlerMethod + "\nto " + mapping + ": There is already '" +
existingHandlerMethod.getBean() + "' bean method\n" + existingHandlerMethod + " mapped.");
}
}
getDirectUrls
找到 mapping 匹配的 url
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#getDirectUrls
private List<String> getDirectUrls(T mapping) {
List<String> urls = new ArrayList<>(1);
// mapping.getPatternsCondition().getPatterns()
for (String path : getMappingPathPatterns(mapping)) {
// 是否匹配
if (!getPathMatcher().isPattern(path)) {
urls.add(path);
}
}
return urls;
}
handlerMethod 和 name 绑定
String name = null;
if (getNamingStrategy() != null) {
// 获取名字
// 类名#方法名
name = getNamingStrategy().getName(handlerMethod, mapping);
// 设置 handlerMethod + name 的关系
addMappingName(name, handlerMethod);
}
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMethodMappingNamingStrategy#getName
@Override
public String getName(HandlerMethod handlerMethod, RequestMappingInfo mapping) {
if (mapping.getName() != null) {
return mapping.getName();
}
StringBuilder sb = new StringBuilder();
// 短类名
String simpleTypeName = handlerMethod.getBeanType().getSimpleName();
for (int i = 0; i < simpleTypeName.length(); i++) {
if (Character.isUpperCase(simpleTypeName.charAt(i))) {
sb.append(simpleTypeName.charAt(i));
}
}
// 组装名称
// 类名+#+方法名称
sb.append(SEPARATOR).append(handlerMethod.getMethod().getName());
return sb.toString();
}
initCorsConfiguration
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#initCorsConfiguration
@Override
protected CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo) {
// 创建 handlerMethod
HandlerMethod handlerMethod = createHandlerMethod(handler, method);
// 获取 beanType
Class<?> beanType = handlerMethod.getBeanType();
// 获取跨域注解 CrossOrigin
CrossOrigin typeAnnotation = AnnotatedElementUtils.findMergedAnnotation(beanType, CrossOrigin.class);
CrossOrigin methodAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, CrossOrigin.class);
if (typeAnnotation == null && methodAnnotation == null) {
return null;
}
// 跨域信息配置
CorsConfiguration config = new CorsConfiguration();
// 更新跨域配置
updateCorsConfig(config, typeAnnotation);
updateCorsConfig(config, methodAnnotation);
if (CollectionUtils.isEmpty(config.getAllowedMethods())) {
// 跨域配置赋给方法
for (RequestMethod allowedMethod : mappingInfo.getMethodsCondition().getMethods()) {
config.addAllowedMethod(allowedMethod.name());
}
}
// 应用跨域
return config.applyPermitDefaultValues();
}
unregister
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#unregister
移除 mapping 信息
执行 map , list 相关的移除方法.
public void unregister(T mapping) {
this.readWriteLock.writeLock().lock();
try {
MappingRegistration<T> definition = this.registry.remove(mapping);
if (definition == null) {
return;
}
this.mappingLookup.remove(definition.getMapping());
for (String url : definition.getDirectUrls()) {
List<T> list = this.urlLookup.get(url);
if (list != null) {
list.remove(definition.getMapping());
if (list.isEmpty()) {
this.urlLookup.remove(url);
}
}
}
removeMappingName(definition);
this.corsLookup.remove(definition.getHandlerMethod());
}
finally {
this.readWriteLock.writeLock().unlock();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论