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. xml
  com/ example/**/applicationContext. xml
  file:C:/ some/ path/*-context. xml
  classpath: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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值