当前位置: 首页 > news >正文

Spring框架中PathMatchingResourcePatternResolver解析资源路径并匹配资源模式

PathMatchingResourcePatternResolver是Spring框架中用于解析资源路径并匹配资源模式的一个工具类。它常用于在Spring的上下文中加载资源,如:配置文件、类路径下的文件加载等。

PathMatchingResourcePatternResolver是一个Ant模式通配符的Resource查找器,可以用来查找类路径下或者文件系统中的资源获取文件系统文件。

PathMatchingResourcePatternResolver位于spring-core包之中。

一、classpath*:前缀文件读取
  • 加载classpath下mapper对应的xml\
    public static void main(String[] args) throws IOException {List<String> list = List.of("classpath*:mapper/mysql/*.xml", "classpath:mapper/oracle/*.xml","classpath:*.properties");List<Resource> resources = getResources(list);System.out.println(resources.size());}public static List<Resource> getResources(List<String> list) throws IOException {PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();List<Resource> resources = new ArrayList<>();for (String path : list) {resources.addAll(List.of(resolver.getResources(path)));}return resources;}

上述案例可以读取到classpath下指定路径的所有xml和properties文件。

二、Ant-style样式通配符

当路径包含Ant-style样式通配符,解析器遵循一个更复杂单已定义的过程来尝试解析通配符,如下:

  / WEB-INF/*-context. xmlcom/ example/**/applicationContext. xmlfile:C:/ some/ path/*-context. xmlclasspath:com/ example/**/applicationContext. xml

示例:

    public static void main(String[] args) throws IOException {List<String> list = List.of("META-INF/**/*.lua", "META-INF/spring/**/*.xml");List<Resource> resources = getResources(list);System.out.println(resources.size());}public static List<Resource> getResources(List<String> list) throws IOException {PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();List<Resource> resources = new ArrayList<>();for (String path : list) {resources.addAll(List.of(resolver.getResources(path)));}return resources;}
三、基于AntPathMatcher的路径匹配
  • Ant风格通配符
?:匹配单个字符
*:匹配0或多个字符
**:匹配0或多个目录
  • 首先创建AntPathMatcher对象
PathMatcher pathMatcher = new AntPathMatcher();
  • 是否是一个Ant模式字符串,即:是否包含通配符字符串
        Assertions.assertFalse(matcher.isPattern("/api/redis"));Assertions.assertFalse(matcher.isPattern("ap/redis/t-a"));Assertions.assertTrue(matcher.isPattern("ap/redis/*"));Assertions.assertTrue(matcher.isPattern("ap/redis/**/a/b"));Assertions.assertTrue(matcher.isPattern("ap/redis/a*"));Assertions.assertTrue(matcher.isPattern("ap/redis/a?"));Assertions.assertTrue(matcher.isPattern("ap/redis/{name}"));
  • url是否匹配指定的Ant模式
        Assertions.assertTrue(matcher.match("api/**/test","api/a/b/test"));Assertions.assertTrue(matcher.match("api/a*b/test","api/aAbcb/test"));Assertions.assertFalse(matcher.match("api/a*b/test","api/aAbcB/test"));Assertions.assertTrue(matcher.match("api/a?b/test","api/acb/test"));Assertions.assertFalse(matcher.match("api/a?b/test","api/acdb/test"));Assertions.assertTrue(matcher.match("api/{name}/test","api/acdb/test"));Assertions.assertFalse(matcher.match("api/a/**/b","api/a/bc"));
  • url是否匹配Ant模式,且以指定的模式开头
        Assertions.assertTrue(matcher.matchStart("api/{name}/test","api/acdb/test"));Assertions.assertTrue(matcher.matchStart("api/a/*","api/a/test"));Assertions.assertTrue(matcher.matchStart("api/a/*","api/a/bc"));Assertions.assertTrue(matcher.matchStart("api/a/**/b","api/a/bc"));Assertions.assertTrue(matcher.matchStart("api/a/**/b","api/a/bc/dd"));Assertions.assertTrue(matcher.matchStart("api/a/*/b","api/a/bc/b"));Assertions.assertFalse(matcher.matchStart("api/a/*/b","api/a/bc/d"));Assertions.assertTrue(matcher.matchStart("api/a/c*","api/a/cbc"));Assertions.assertFalse(matcher.matchStart("api/a/c*","a/api/a/cbc"));Assertions.assertFalse(matcher.matchStart("api/a/c*","api/a/c/dd"));
  • 提取Ant通配符匹配开始之后的路由,支持‘*’、‘**’、‘?’
        Assertions.assertEquals(matcher.extractPathWithinPattern("/docs/cvs/commit.html","/docs/cvs/commit.html"),"");Assertions.assertEquals(matcher.extractPathWithinPattern("/docs/*","/docs/cvs/commit"),"cvs/commit");Assertions.assertEquals(matcher.extractPathWithinPattern("/docs/cvs/*.html","/docs/cvs/commit.html"),"commit.html");Assertions.assertEquals(matcher.extractPathWithinPattern("/docs/**","/docs/cvs/commit"),"cvs/commit");Assertions.assertEquals(matcher.extractPathWithinPattern("/docs/**\\/*.html","/docs/cvs/commit.html"),"cvs/commit.html");Assertions.assertEquals(matcher.extractPathWithinPattern("/*.html","/docs/cvs/commit.html"),"docs/cvs/commit.html");Assertions.assertEquals(matcher.extractPathWithinPattern("*.html","/docs/cvs/commit.html"),"/docs/cvs/commit.html");Assertions.assertEquals(matcher.extractPathWithinPattern("*","/docs/cvs/commit.html"),"/docs/cvs/commit.html");Assertions.assertEquals(matcher.extractPathWithinPattern("/a/b?","/a/bc/d"),"bc/d");
  • 提取Ant风格url中变量
        Map<String,String> uriTemplate = matcher.extractUriTemplateVariables("api/{userid}/{id}","api/1002356/11");Assertions.assertEquals(uriTemplate.get("userid"),"1002356");Assertions.assertEquals(uriTemplate.get("id"),"11");
  • 将两个URL拼接成一个新的url
        Assertions.assertEquals(matcher.combine(null, null),"");Assertions.assertEquals(matcher.combine("/hotels", null),"/hotels");Assertions.assertEquals(matcher.combine(null, "/hotels"),"/hotels");Assertions.assertEquals(matcher.combine("/hotels", "/bookings"),"/hotels/bookings");Assertions.assertEquals(matcher.combine("/hotels", "bookings"),"/hotels/bookings");Assertions.assertEquals(matcher.combine("/hotels/*", "bookings"),"/hotels/bookings");Assertions.assertEquals(matcher.combine("/hotels/**", "/bookings"),"/hotels/**/bookings");Assertions.assertEquals(matcher.combine("/hotels", "{hotel}"),"/hotels/{hotel}");Assertions.assertEquals(matcher.combine("/hotels/*", "{hotel}"),"/hotels/{hotel}");Assertions.assertEquals(matcher.combine("/hotels/**", "{hotel}"),"/hotels/**/{hotel}");Assertions.assertEquals(matcher.combine("/*.html", "/hotels.html"),"/hotels.html");Assertions.assertEquals(matcher.combine("/*.html", "/hotels"),"/hotels.html");Assertions.assertThrows(IllegalArgumentException.class,()->matcher.combine("/*.html", "/*.txt"));
  • 给定一个url返回一个可以按照显式顺序排序的Comparator对象
        Comparator<String> comparator1 = matcher.getPatternComparator("/hotels/new");Comparator<String> comparator2 = matcher.getPatternComparator("/hotels/{hotel}");Comparator<String> comparator3 = matcher.getPatternComparator("/hotels/*");

开源SDK:https://github.com/mingyang66/spring-parent


http://www.mrgr.cn/news/15444.html

相关文章:

  • SQL 对版本进行排序遇到的问题
  • 构建高可用性Nginx:配置策略与最佳实践
  • java中的Opencv:Opencv简介与开发环境部署
  • pnpm国内源设置
  • Nginx负载均衡中的连接超时处理:策略与配置
  • Java中的分布式一致性与共识算法
  • 【Git】在 Windows 和 Linux 上配置 SSH 密钥并初始化 Git 项目
  • 脚手架工具的应用(前端和后端搭建)
  • nginx 负载均衡详解与实现方法案例
  • SQL 语句及其分类
  • 鸿蒙HarmonyOS之使用preferences首选项保存获取数据
  • 三行五行的 SQL 只存在于教科书和培训班
  • STL容器中 list(双向链表)的增删改查
  • 什么是跨域问题?出现的原因和解决方法是什么?
  • 记录一次两台虚拟机Oracle rac 心跳不能建立的排查
  • 【对象存储】MINIO_RELEASE.2024-08-17T01-24-54Z-cpuv1部署与操作
  • 2024.8.26 Python,最大子数和与动态规划,最小路径和,分割回文串,字典序排数,最长重复子数组(动态规划)
  • 斯坦福大学cs231n (图像分类)
  • Android如何高效的加载大型位图
  • 【JVM】执行引擎、JIT、逃逸分析(二)