Spring源码解析 - AntPathMatcher
2016-02-24 15:37
183 查看
最近在看SpringMVC的源码,发现request分发时,路径匹配最后是委托给AntPathMatcher实现的.索性看看吧.
文章摘要:
1. ant匹配规则
2. PathMatcher接口
3. 通过测试用例看AntPathMatcher的使用
ant匹配规则
AntPathMatcher如名使用的ant 的匹配规则,我们先看看吧.
字符wildcard 描述
? 匹配一个字符
* 匹配0个及以上字符
** 匹配0个及以上目录directories
看几个官方的例子吧:
com/t?st.jsp - 匹配: com/test.jsp , com/tast.jsp , com/txst.jsp
com/*.jsp - 匹配: com文件夹下的全部.jsp文件
com/**/test.jsp - 匹配: com文件夹和子文件夹下的全部.jsp文件,
org/springframework/**/*.jsp - 匹配: org/springframework文件夹和子文件夹下的全部.jsp文件
org/**/servlet/bla.jsp - 匹配: org/springframework/servlet/bla.jsp , org/springframework/testing/servlet/bla.jsp , org/servlet/bla.jsp
PathMatcher接口
主要是判断是否匹配pattern,并解析出path中的参数
通过测试用例看AntPathMatcher的使用
一看测试用例,瞬间服了,人家开发真是规范.
![](http://images2015.cnblogs.com/blog/157441/201602/157441-20160224111543615-567976928.png)
人家整这么规范,还是有空直接看源码好了.这边挑几个简单的例子看看就好
1. match 跟 matchStart 的差异,这个我们在测试用例看下面的情况会比较明确
这边的代码,我截取了一小部分
2. extractPathWithinPattern,代码很清楚,不废话
3. extractUriTemplateVariables
4. combine
文章摘要:
1. ant匹配规则
2. PathMatcher接口
3. 通过测试用例看AntPathMatcher的使用
ant匹配规则
AntPathMatcher如名使用的ant 的匹配规则,我们先看看吧.
字符wildcard 描述
? 匹配一个字符
* 匹配0个及以上字符
** 匹配0个及以上目录directories
看几个官方的例子吧:
com/t?st.jsp - 匹配: com/test.jsp , com/tast.jsp , com/txst.jsp
com/*.jsp - 匹配: com文件夹下的全部.jsp文件
com/**/test.jsp - 匹配: com文件夹和子文件夹下的全部.jsp文件,
org/springframework/**/*.jsp - 匹配: org/springframework文件夹和子文件夹下的全部.jsp文件
org/**/servlet/bla.jsp - 匹配: org/springframework/servlet/bla.jsp , org/springframework/testing/servlet/bla.jsp , org/servlet/bla.jsp
PathMatcher接口
主要是判断是否匹配pattern,并解析出path中的参数
package org.springframework.util; public interface PathMatcher { /** * 判断传入的path是否可以作为pattern使用 */ boolean isPattern(String path); /** * 使用pattern匹配path */ boolean match(String pattern, String path); /** * 如名,是否开始部分匹配 */ boolean matchStart(String pattern, String path); /** * 提取path中匹配到的部分,如pattern(myroot/*.html),path(myroot/myfile.html),返回myfile.html */ String extractPathWithinPattern(String pattern, String path); /** * 提取path中匹配到的部分,只是这边还需跟占位符配对为map, * 如pattern(/hotels/{hotel}),path(/hotels/1),解析出"hotel"->"1" */ Map<String, String> extractUriTemplateVariables(String pattern, String path); /** * 提供比较器 */ Comparator<String> getPatternComparator(String path); /** * 合并pattern,pattern1然后pattern2 */ String combine(String pattern1, String pattern2); }
通过测试用例看AntPathMatcher的使用
一看测试用例,瞬间服了,人家开发真是规范.
![](http://images2015.cnblogs.com/blog/157441/201602/157441-20160224111543615-567976928.png)
人家整这么规范,还是有空直接看源码好了.这边挑几个简单的例子看看就好
1. match 跟 matchStart 的差异,这个我们在测试用例看下面的情况会比较明确
这边的代码,我截取了一小部分
package org.springframework.util; public class AntPathMatcherTests { @Test public void match() { // ... assertFalse(pathMatcher.match("/x/x/**/bla", "/x/x/x/")); // ... } @Test public void withMatchStart() { // ... assertTrue(pathMatcher.matchStart("/x/x/**/bla", "/x/x/x/")); // ... } }
2. extractPathWithinPattern,代码很清楚,不废话
package org.springframework.util; public class AntPathMatcherTests { @Test public void extractPathWithinPattern() throws Exception { // ... assertEquals("", pathMatcher.extractPathWithinPattern("/docs/commit.html", "/docs/commit.html")); assertEquals("cvs/commit", pathMatcher.extractPathWithinPattern("/docs/*", "/docs/cvs/commit")); assertEquals("docs/cvs/commit", pathMatcher.extractPathWithinPattern("/d?cs/*", "/docs/cvs/commit")); // ... } }
3. extractUriTemplateVariables
package org.springframework.util; public class AntPathMatcherTests { @Test public void extractUriTemplateVariables() throws Exception { Map<String, String> result = pathMatcher.extractUriTemplateVariables("/hotels/{hotel}", "/hotels/1"); assertEquals(Collections.singletonMap("hotel", "1"), result); // ... result = pathMatcher.extractUriTemplateVariables("/{page}.*", "/42.html"); assertEquals(Collections.singletonMap("page", "42"), result); // ... } /** * SPR-7787 */ @Test public void extractUriTemplateVarsRegexQualifiers() { Map<String, String> result = pathMatcher.extractUriTemplateVariables( "{symbolicName:[\\p{L}\\.]+}-sources-{version:[\\p{N}\\.]+}.jar", "com.example-sources-1.0.0.jar"); assertEquals("com.example", result.get("symbolicName")); assertEquals("1.0.0", result.get("version")); // ... } }
4. combine
package org.springframework.util; public class AntPathMatcherTests { @Test public void combine() { // ... assertEquals("/hotels", pathMatcher.combine("/hotels", null)); assertEquals("/hotels/booking", pathMatcher.combine("/hotels/*", "/booking")); assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "booking")); assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "/booking")); assertEquals("/hotels/booking", pathMatcher.combine("/hotels", "/booking")); assertEquals("/hotels/{hotel}", pathMatcher.combine("/hotels/*", "{hotel}")); assertEquals("/hotels/**/{hotel}", pathMatcher.combine("/hotels/**", "{hotel}")); assertEquals("/hotels/*/booking/{booking}", pathMatcher.combine("/hotels/*/booking", "{booking}")); } }
相关文章推荐
- spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途
- Java泛型通配符解惑
- Java 泛型的理解与总结
- java枚举使用详解
- Structs开发整理
- js模仿java格式化字符串处理
- Struts,Spring,Hibernate的作用
- 【java基础】List
- MyEclipse10.7.1解决项目打war包失败
- Struts2 struts标签
- Java IO技术
- Java基础03 构造器与方法重载
- RxJava操作符-subscribeOn
- (JavaCard) 技术入门 2 -- 搭建JCDK开发环境-Java Card 3.0.5
- java便捷开发
- Spring-web源码解析之Filter-AbstractRequestLoggingFilter
- java装饰者模式
- Eclipse批量修改
- (JavaCard) 技术入门 1 -- 名词普及
- spring No default constructor found; nested exception is java.lang.NoSuchMethodException: com.slj.mo