SpringMVC源码情操陶冶-InterceptorsBeanDefinitionParser拦截器解析器
解析mvc:interceptors节点
观察下InterceptorsBeanDefinitionParser的源码备注
/**
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses a
* {@code interceptors} element to register a set of {@link MappedInterceptor} definitions.
*
* @author Keith Donald
* @since 3.0
*/
从官方注释可知,此解析器的作用是解析mvc:interceptors
节点并注册成MappedInterceptors
Definition集合
mvc:interceptors的配置使用
<!-- 拦截器设置 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<!-- 静态资源不拦截 -->
<mvc:exclude-mapping path="/mobile/**"/>
<mvc:exclude-mapping path="/pc/**"/>
<!-- 主页不拦截 -->
<!-- 特殊user资源获取不拦截 -->
<bean class="com.du.wx.interceptor.UserInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
InterceptorsBeanDefinitionParser#parse
直接观察公用接口方法parse()
,源码奉上
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
//CompositeComponentDefinition表示其可以装载多个ComponentDefinition
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), parserContext.extractSource(element));
parserContext.pushContainingComponent(compDefinition);
//允许mvc:interceptors拥有path-matcher属性,表示 路径匹配解析器
RuntimeBeanReference pathMatcherRef = null;
if (element.hasAttribute("path-matcher")) {
pathMatcherRef = new RuntimeBeanReference(element.getAttribute("path-matcher"));
}
//查询mvc:interceptors节点下的bean/ref/interceptor标签
List<Element> interceptors = DomUtils.getChildElementsByTagName(element, "bean", "ref", "interceptor");
for (Element interceptor : interceptors) {
//采用MappedInterceptor作为beanClass
RootBeanDefinition mappedInterceptorDef = new RootBeanDefinition(MappedInterceptor.class);
mappedInterceptorDef.setSource(parserContext.extractSource(interceptor));
mappedInterceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
ManagedList<String> includePatterns = null;
ManagedList<String> excludePatterns = null;
Object interceptorBean;
//解析mvc:interceptor节点
if ("interceptor".equals(interceptor.getLocalName())) {
//解析mvc:mapping节点中的path属性,保存里面的拦截路径集合
includePatterns = getIncludePatterns(interceptor, "mapping");
//解析mvc:exclude-mapping节点中的path属性,保存里面的放行路径集合
excludePatterns = getIncludePatterns(interceptor, "exclude-mapping");
//解析bean标签/ref标签,并封装成beanDefinition
Element beanElem = DomUtils.getChildElementsByTagName(interceptor, "bean", "ref").get(0);
interceptorBean = parserContext.getDelegate().parsePropertySubElement(beanElem, null);
}
else {
//解析bean标签/ref标签,并封装成beanDefinition
interceptorBean = parserContext.getDelegate().parsePropertySubElement(interceptor, null);
}
//MappedInterceptor类的构造函数可接受三个参数
/**
**public MappedInterceptor(String[] includePatterns, String[] excludePatterns, HandlerInterceptor interceptor)
**
*/
mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, includePatterns);
mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, excludePatterns);
mappedInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(2, interceptorBean);
//为MappedInterceptor添加pathMatcher属性
if (pathMatcherRef != null) {
mappedInterceptorDef.getPropertyValues().add("pathMatcher", pathMatcherRef);
}
//保存到spring的bean工厂中
String beanName = parserContext.getReaderContext().registerWithGeneratedName(mappedInterceptorDef);
parserContext.registerComponent(new BeanComponentDefinition(mappedInterceptorDef, beanName));
}
parserContext.popAndRegisterContainingComponent();
return null;
}
小结
- 解析
mvc:interceptors
,其会封装为MappedInterceptor
类,进而保存
- 拦截路径匹配集合
includePatterns
- 不拦截路径匹配集合
excludePatterns
- 拦截处理器
interceptor
,其必须是HandlerInterceptor
的实现类- 路径匹配处理器,如果
mvc:interceptors
设定了path-matcher
属性,默认为AntPathMatcher
MappedInterceptor
其内部包含了HandlerInterceptor
集合,并添加了路径的映射属性。对匹配的路径调用相应的HandlerInterceptors
mvc:interceptors
注册的拦截器在AbstractHandlerMapping
类中通过matches()
方法被调用,具体调用见Springmvc源码
SpringMVC源码情操陶冶-InterceptorsBeanDefinitionParser拦截器解析器的更多相关文章
- SpringMVC源码情操陶冶-ResourcesBeanDefinitionParser静态资源解析器
解析mvc:resources节点,控制对静态资源的映射访问 查看官方注释 /** * {@link org.springframework.beans.factory.xml.BeanDefinit ...
- Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器
本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析 源码概览 对BeanDefinitionParser接口的 ...
- Spring源码情操陶冶-ComponentScanBeanDefinitionParser文件扫描解析器
承接前文Spring源码情操陶冶-自定义节点的解析,本文讲述spring通过context:component-scan节点干了什么事 ComponentScanBeanDefinitionParse ...
- Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器
本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...
- Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器
aop-Aspect Oriented Programming,面向切面编程.根据百度百科的解释,其通过预编译方式和运行期动态代理实现程序功能的一种技术.主要目的是为了程序间的解耦,常用于日志记录.事 ...
- SpringMVC源码情操陶冶-HandlerAdapter适配器简析
springmvc中对业务的具体处理是通过HandlerAdapter适配器操作的 HandlerAdapter接口方法 列表如下 /** * Given a handler instance, re ...
- Spring源码情操陶冶-自定义节点的解析
本文承接前文Spring源码情操陶冶-DefaultBeanDefinitionDocumentReader#parseBeanDefinitions,特开辟出一块新地来啃啃这块有意思的骨头 自定义节 ...
- SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器
mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...
- SpringMVC源码情操陶冶#task-executor解析器
承接Spring源码情操陶冶-自定义节点的解析.线程池是jdk的一个很重要的概念,在很多的场景都会应用到,多用于处理多任务的并发处理,此处借由spring整合jdk的cocurrent包的方式来进行深 ...
随机推荐
- COGS 862. 二进制数01串【dp+经典二分+字符串】
862. 二进制数01串 ★ 输入文件:kimbits.in 输出文件:kimbits.out 简单对比 时间限制:1 s 内存限制:128 MB USACO/kimbits(译 by ...
- c++(hash表)
hash表,有时候也被称为散列表.个人认为,hash表是介于链表和二叉树之间的一种中间结构.链表使用十分方便,但是数据查找十分麻烦:二叉树中的数据严格有序,但是这是以多一个指针作为代价的结果.hash ...
- c++(排序二叉树)
前面我们讲过双向链表的数据结构.每一个循环节点有两个指针,一个指向前面一个节点,一个指向后继节点,这样所有的节点像一颗颗珍珠一样被一根线穿在了一起.然而今天我们讨论的数据结构却有一点不同,它有三个节点 ...
- 用于 C♯ 图像识别的轮廓分析技术
用于 C♯ 图像识别的轮廓分析技术 供稿:Conmajia 标题:Contour Analysis for Image Recognition in C# 作者:Pavel Torgashov 此中文 ...
- 什么是A记录/CNAME记录/MX记录/TXT记录
答: A 记录(Address)是用来指定主机名(或域名)对应的IP地址记录.当你输入域名的时候给你引导向设置在DNS的A记录所对应的服务器. CNAME记录 ( Canonical Name )是一 ...
- 独立服务器 云主机、VPS以及虚拟主机三者之间的区别是什么?哪个更好?
https://www.zhihu.com/question/21442353#answer-2442764 云主机(如 EC2,[1] )和 VPS (如 Linode,[2])都是完整的操作系统( ...
- MySQL密码忘了怎么办?MySQL重置root密码方法
本文主要介绍Windows和Linux系统下忘记密码重置root密码的方法,需要的朋友可以参考下. MySQL有时候忘记了root密码是一件伤感的事.这里提供Windows 和 Linux 下的密码重 ...
- 新版Azure Automation Account 浅析(二) --- 更新Powershell模块和创建Runbook
前篇我们讲了怎样创建一个自动化账户以及创建时候"Run As Account"选项背后的奥秘.这一篇针对在Azure自动化账户中使用Powershell Runbook的用户讲一下 ...
- Mysql Order By 字符串排序,mysql 字符串order by
Mysql Order By 字符串排序,mysql 字符串order by ============================== ©Copyright 蕃薯耀 2017年9月30日 http ...
- JS中获取session中传过来的值对象
摘录自:http://www.360doc.com/content/11/0316/13/5790498_101627263.shtml 把某一对象置于session范围内,并在JSP页面中提取ses ...