解析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节点并注册成MappedInterceptorsDefinition集合

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类,进而保存
  1. 拦截路径匹配集合includePatterns
  2. 不拦截路径匹配集合excludePatterns
  3. 拦截处理器interceptor,其必须是HandlerInterceptor的实现类
  4. 路径匹配处理器,如果mvc:interceptors设定了path-matcher属性,默认为AntPathMatcher
  • MappedInterceptor其内部包含了HandlerInterceptor集合,并添加了路径的映射属性。对匹配的路径调用相应的HandlerInterceptors

  • mvc:interceptors注册的拦截器在AbstractHandlerMapping类中通过matches()方法被调用,具体调用见Springmvc源码

SpringMVC源码情操陶冶-InterceptorsBeanDefinitionParser拦截器解析器的更多相关文章

  1. SpringMVC源码情操陶冶-ResourcesBeanDefinitionParser静态资源解析器

    解析mvc:resources节点,控制对静态资源的映射访问 查看官方注释 /** * {@link org.springframework.beans.factory.xml.BeanDefinit ...

  2. Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器

    本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析 源码概览 对BeanDefinitionParser接口的 ...

  3. Spring源码情操陶冶-ComponentScanBeanDefinitionParser文件扫描解析器

    承接前文Spring源码情操陶冶-自定义节点的解析,本文讲述spring通过context:component-scan节点干了什么事 ComponentScanBeanDefinitionParse ...

  4. Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器

    本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...

  5. Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器

    aop-Aspect Oriented Programming,面向切面编程.根据百度百科的解释,其通过预编译方式和运行期动态代理实现程序功能的一种技术.主要目的是为了程序间的解耦,常用于日志记录.事 ...

  6. SpringMVC源码情操陶冶-HandlerAdapter适配器简析

    springmvc中对业务的具体处理是通过HandlerAdapter适配器操作的 HandlerAdapter接口方法 列表如下 /** * Given a handler instance, re ...

  7. Spring源码情操陶冶-自定义节点的解析

    本文承接前文Spring源码情操陶冶-DefaultBeanDefinitionDocumentReader#parseBeanDefinitions,特开辟出一块新地来啃啃这块有意思的骨头 自定义节 ...

  8. SpringMVC源码情操陶冶-AnnotationDrivenBeanDefinitionParser注解解析器

    mvc:annotation-driven节点的解析器,是springmvc的核心解析器 官方注释 Open Declaration org.springframework.web.servlet.c ...

  9. SpringMVC源码情操陶冶#task-executor解析器

    承接Spring源码情操陶冶-自定义节点的解析.线程池是jdk的一个很重要的概念,在很多的场景都会应用到,多用于处理多任务的并发处理,此处借由spring整合jdk的cocurrent包的方式来进行深 ...

随机推荐

  1. C++STL vector简单使用练习1

    #include <iostream> #include <vector> #include <numeric> using namespace std; int ...

  2. Jungle Roads(kruskar)

    Jungle Roads 题目链接;http://poj.org/problem?id=1251 Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  3. BASH 学习笔记小结

    1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...

  4. cdn和反向代理

    都是用来加速网站访问速度 cdn和反向代理的基本原理都是使用缓存,区别在于cdn部署在网络提供商的机房,使用户在请求网络服务时,可以从距离自己最近的网络提供商机房获取数据:而反向代理则部署在网站的中心 ...

  5. 【端-iOS】给iOS开发入门者编码的一点建议

    规范编码可以提高代码的可读性,降低维护成本.作为一个程序员,要对自己写的代码负责,虽然bug无可避免,但是写代码时最基本的编码规则还是应该遵守的,否则不是坑自己就是坑别人,因为代码肯定是要维护的. 下 ...

  6. iframe及与页面之间的通信

    获取iframe对象 iframe元素本身是位于父级页面中的,所以你可以像一个普通元素一样的使用和操作它 代表了iframe内容window对象是作为一个页面的属性加入到iframe中的, 为了让父级 ...

  7. kafka producer生产数据到kafka异常:Got error produce response with correlation id 16 on topic-partition...Error: NETWORK_EXCEPTION

      kafka producer生产数据到kafka异常:Got error produce response with correlation id 16 on topic-partition... ...

  8. Web应用的目录结构

    Web应用的目录结构 |- WebRoot :   web应用的根目录 |- 静态资源(html+css+js+image+vedio)|- WEB-INF :固定写法. |-classes: (可选 ...

  9. HTTP协议简介

    一.简介 HTTP(HyperText Transfer Protocol, 超文本传输协议) 是访问互联网使用的核心通信协议,也是所有web应用程序使用的通信协议.消息模型:客户端发送请求消息,服务 ...

  10. mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)

    REPAIR TABLE `table_name` 修复表 OPTIMIZE TABLE `table_name` 优化表 show create table tablename   表结构 REPA ...