资源匹配类

PathMatchingResourcePatternResolver

用于解析指定的一个或多个匹配资源路径,资源路径可以是唯一的,也可以是通过classpath 指定的,也支持Ant风格的特殊表达式。

Ant风格的类型表达式

支持的地址匹配前缀有:

  1. classpath

    classpath:conf/conf.xml,从类路径下的conf目录中加载资源,匹配conf.xml文件

    在classpath后添加星号可匹配所有类路径下的资源,classpath*

  2. file

    file:/conf/conf.xml,从文件系统中匹配资源,稳定性不好

  3. http

    http://www.example.com/conf.xml ,从web服务器中加载资源

  4. ftp

    ftp://www.example.com/conf.xml ,从发图片服务器中加载资源

常见方法应用

PathMatchingResourcePatternResolver

// 匹配符合条件的多个资源
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml");
// 匹配符号条件的单个资源,方法不加s
Resource resource = new PathMatchingResourcePatternResolver().getResource("classpath:mapper/*.xml");
// 匹配所有类路径根目录下以xml类型的文件
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:*.xml");