4.4.1  使用路径通配符加载Resource

前面介绍的资源路径都是非常简单的一个路径匹配一个资源,Spring还提供了一种更强大的Ant模式通配符匹配,从能一个路径匹配一批资源。

Ant路径通配符支持“?”、“*”、“**”,注意通配符匹配不包括目录分隔符“/”:

“?”:匹配一个字符,如“config?.xml”将匹配“config1.xml”;

“*”:匹配零个或多个字符串,如“cn/*/config.xml”将匹配“cn/javass/config.xml”,但不匹配匹配“cn/config.xml”;而“cn/config-*.xml”将匹配“cn/config-dao.xml”;

“**”:匹配路径中的零个或多个目录, 如“cn/**/config.xml”将匹配“cn /config.xml”,也匹配“cn/javass/spring/config.xml”;而“cn/javass/config-**.xml” 将匹配“cn/javass/config-dao.xml”,即把“**”当做两个“*”处理。

Spring提供AntPathMatcher来进行Ant风格的路径匹配。具体测试请参考cn.javass.spring.chapter4. AntPathMatcherTest。

Spring在加载类路径资源时除了提供前缀“classpath:”的来支持加载一个Resource,还提供一个前缀“classpath*:”来支持加载所有匹配的类路径Resource。

Spring 提供ResourcePatternResolver接口来加载多个Resource,该接口继承了ResourceLoader并添加了 “Resource[] getResources(String locationPattern)”用来加载多个Resource:

public interface ResourcePatternResolver extends ResourceLoader {
String CLASSPATH_ALL_URL_PREFIX = "classpath*:";
Resource[] getResources(String locationPattern) throws IOException;
}

Spring提供了一个ResourcePatternResolver实现PathMatchingResourcePatternResolver, 它是基于模式匹配的,默认使用AntPathMatcher进行路径匹配,它除了支持ResourceLoader支持的前缀外,还额外支持 “classpath*:”用于加载所有匹配的类路径Resource,ResourceLoader不支持前缀“classpath*:”:

首 先做下准备工作,在项目的“resources”创建“META-INF”目录,然后在其下创建一个“INDEX.LIST”文件。同时在 “org.springframework.beans-3.0.5.RELEASE.jar”和 “org.springframework.context-3.0.5.RELEASE.jar”两个jar包里也存在相同目录和文件。然后创建一个 “LICENSE”文件,该文件存在于“com.springsource.cn.sf.cglib-2.2.0.jar”里。

一、“classpath”: 用于加载类路径(包括jar包)中的一个且仅一个资源;对于多个匹配的也只返回一个,所以如果需要多个匹配的请考虑“classpath*:”前缀;

@Test
public void testClasspathPrefix() throws IOException {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//只加载一个绝对匹配Resource,且通过ResourceLoader.getResource进行加载
Resource[] resources=resolver.getResources("classpath:META-INF/INDEX.LIST");
Assert.assertEquals(1, resources.length);
//只加载一个匹配的Resource,且通过ResourceLoader.getResource进行加载
resources = resolver.getResources("classpath:META-INF/*.LIST");
Assert.assertTrue(resources.length == 1);
}

二、“classpath*”: 用于加载类路径(包括jar包)中的所有匹配的资源。带通配符的classpath使用“ClassLoader”的“Enumeration<URLgetResources(String name)”方法来查找通配符之前的资源,然后通过模式匹配来获取匹配的资源。如“classpath:META-INF/*.LIST”将首先加载通配符之前的目录“META-INF”,然后再遍历路径进行子路径匹配从而获取匹配的资源。

@Test
public void testClasspathAsteriskPrefix () throws IOException {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//将加载多个绝对匹配的所有Resource
//将首先通过ClassLoader.getResources("META-INF")加载非模式路径部分
//然后进行遍历模式匹配
Resource[] resources=resolver.getResources("classpath*:META-INF/INDEX.LIST");
Assert.assertTrue(resources.length > 1);
//将加载多个模式匹配的Resource
resources = resolver.getResources("classpath*:META-INF/*.LIST");
Assert.assertTrue(resources.length > 1);
}

注意“resources.length >1”说明返回多个Resource。不管模式匹配还是非模式匹配只要匹配的都将返回。

在“com.springsource.cn.sf.cglib-2.2.0.jar”里包含“asm-license.txt”文件,对于使用 “classpath*: asm-*.txt”进行通配符方式加载资源将什么也加载不了“asm-license.txt”文件,注意一定是模式路径匹配才会遇到这种问题。这是由 于“ClassLoader”的“getResources(String name)”方法的限制,对于name为“”的情况将只返回文件系统的类路径,不会包换jar包根路径。

    @Test
public void testClasspathAsteriskPrefixLimit() throws IOException {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //将首先通过ClassLoader.getResources("")加载目录,
//将只返回文件系统的类路径不返回jar的跟路径
//然后进行遍历模式匹配
Resource[] resources = resolver.getResources("classpath*:asm-*.txt");
Assert.assertTrue(resources.length == 0);
//将通过ClassLoader.getResources("asm-license.txt")加载
//asm-license.txt存在于com.springsource.net.sf.cglib-2.2.0.jar
resources = resolver.getResources("classpath*:asm-license.txt");
Assert.assertTrue(resources.length > 0);
//将只加载文件系统类路径匹配的Resource
resources = resolver.getResources("classpath*:LICENS*");
Assert.assertTrue(resources.length == 1);
}

对 于“resolver.getResources("classpath*:asm-*.txt");”,由于在项目“resources”目录下没有所 以应该返回0个资源;“resolver.getResources("classpath*:asm-license.txt");”将返回jar包里 的Resource;“resolver.getResources("classpath*:LICENS*");”,因为将只返回文件系统类路径资 源,所以返回1个资源。

因此加载通配符路径时(即路径中包含通配符),必须包含一个根目录才能保证加载的资源是所有的,而不是部分。

三、“file”:加载一个或多个文件系统中的Resource。如“file:D:/*.txt”将返回D盘下的所有txt文件;

四、无前缀:通过ResourceLoader实现加载一个资源。

AppliacationContext提供的getResources方法将获取资源委托给ResourcePatternResolver实现,默认使用PathMatchingResourcePatternResolver。所有在此就无需介绍其使用方法了。

4.4.2  注入Resource数组

Spring还支持注入Resource数组,直接看配置如下:

    <bean id="resourceBean1" class="cn.javass.spring.chapter4.bean.ResourceBean4">
<property name="resources">
<array>
<value>cn/javass/spring/chapter4/test1.properties</value>
<value>log4j.xml</value>
</array>
</property>
</bean>
<bean id="resourceBean2" class="cn.javass.spring.chapter4.bean.ResourceBean4">
<property name="resources" value="classpath*:META-INF/INDEX.LIST"/>
</bean>
<bean id="resourceBean3" class="cn.javass.spring.chapter4.bean.ResourceBean4">
<property name="resources">
<array>
<value>cn/javass/spring/chapter4/test1.properties</value>
<value>classpath*:META-INF/INDEX.LIST</value>
</array>
</property>
</bean>

“resourceBean1”就不用多介绍了,传统实现方式;对于“resourceBean2”则使用前缀“classpath*”,看到这大家应该懂的,加载匹配多个资源;“resourceBean3”是混合使用的;

Spring通过ResourceArrayPropertyEditor来进行类型转换的,而它又默认使用 “PathMatchingResourcePatternResolver”来进行把路径解析为Resource对象。所有大家只要会使用 “PathMatchingResourcePatternResolver”,其它一些实现都是委托给它的,比如 AppliacationContext的“getResources”方法等。

4.4.3  AppliacationContext实现对各种Resource的支持

一、ClassPathXmlApplicationContext默认将通过classpath进行加载返回ClassPathResource,提供两类构造器方法:

    public class ClassPathXmlApplicationContext {
//1)通过ResourcePatternResolver实现根据configLocation获取资源
public ClassPathXmlApplicationContext(String configLocation);
public ClassPathXmlApplicationContext(String... configLocations);
public ClassPathXmlApplicationContext(String[] configLocations, ……); //2)通过直接根据path直接返回ClasspathResource
public ClassPathXmlApplicationContext(String path, Class clazz);
public ClassPathXmlApplicationContext(String[] paths, Class clazz);
public ClassPathXmlApplicationContext(String[] paths, Class clazz, ……);
}

第一类构造器是根据提供的配置文件路径使用“ResourcePatternResolver ”的“getResources()”接口通过匹配获取资源;即如“classpath:config.xml”

第二类构造器则是根据提供的路径和clazz来构造ClassResource资源。即采用“public ClassPathResource(String path, Class<?> clazz)”构造器获取资源。

       二、FileSystemXmlApplicationContext将 加载相对于当前工作目录的“configLocation”位置的资源,注意在linux系统上不管“configLocation”是否带“/”,都作 为相对路径;而在window系统上如“D:/resourceInject.xml”是绝对路径。因此在除非很必要的情况下,不建议使用该 ApplicationContext。

    public class FileSystemXmlApplicationContext{
public FileSystemXmlApplicationContext(String configLocation);
public FileSystemXmlApplicationContext(String... configLocations,……);
}
    //linux系统,以下全是相对于当前vm路径进行加载
new FileSystemXmlApplicationContext("chapter4/config.xml");
new FileSystemXmlApplicationContext("/chapter4/confg.xml");
    //windows系统,第一个将相对于当前vm路径进行加载;
//第二个则是绝对路径方式加载
new FileSystemXmlApplicationContext("chapter4/config.xml");
new FileSystemXmlApplicationContext("d:/chapter4/confg.xml");

此处还需要注意:在linux系统上,构造器使用的是相对路径,而ctx.getResource()方法如果以“/”开头则表示获取绝对路径资源,而不带前导“/”将返回相对路径资源。如下:

    //linux系统,第一个将相对于当前vm路径进行加载;
//第二个则是绝对路径方式加载
ctx.getResource ("chapter4/config.xml");
ctx.getResource ("/root/confg.xml");
//windows系统,第一个将相对于当前vm路径进行加载;
//第二个则是绝对路径方式加载
ctx.getResource ("chapter4/config.xml");
ctx.getResource ("d:/chapter4/confg.xml");

因此如果需要加载绝对路径资源最好选择前缀“file”方式,将全部根据绝对路径加载。如在linux系统“ctx.getResource ("file:/root/confg.xml");”

开涛spring3(4.4) - 资源 之 4.4 Resource通配符路径的更多相关文章

  1. 资源 之 4.4 Resource通配符路径 ——跟我学spring3

    4.4.1  使用路径通配符加载Resource 前面介绍的资源路径都是非常简单的一个路径匹配一个资源,Spring还提供了一种更强大的Ant模式通配符匹配,从能一个路径匹配一批资源. Ant路径通配 ...

  2. 资源 之 4.4 Resource通配符路径(拾贰)

    4.4.1  使用路径通配符加载Resource 前面介绍的资源路径都是非常简单的一个路径匹配一个资源,Spring还提供了一种更强大的Ant模式通配符匹配,从能一个路径匹配一批资源. Ant路径通配 ...

  3. 开涛spring3(4.2) - 资源 之 4.2 内置Resource实现

    4.2  内置Resource实现 4.2.1  ByteArrayResource ByteArrayResource代表byte[]数组资源,对于“getInputStream”操作将返回一个By ...

  4. 开涛spring3(4.1) - 资源 之 4.1 基础知识

    4.1.1  概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源.File资源资源.ClassPath相关资源.服务器相关资源 (JBoss AS 5.x上的VFS资源)等 ...

  5. 开涛spring3(4.3) - 资源 之 4.3 访问Resource

    4.3.1  ResourceLoader接口 ResourceLoader接口用于返回Resource对象:其实现可以看作是一个生产Resource的工厂类. public interface Re ...

  6. 开涛spring3(1) - Spring概述

    1.1.1  Spring是什么 Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发.应用程序是由 ...

  7. 开涛spring3(12.2) - 零配置 之 12.2 注解实现Bean依赖注入

    12.2  注解实现Bean依赖注入 12.2.1  概述 注解实现Bean配置主要用来进行如依赖注入.生命周期回调方法定义等,不能消除XML文件中的Bean元数据定义,且基于XML配置中的依赖注入的 ...

  8. 开涛spring3(9.2) - Spring的事务 之 9.2 数据库事务概述

    9.2.1  概述 Spring框架支持事务管理的核心是事务管理器抽象,对于不同的数据访问框架(如Hibernate)通过实现策略接口 PlatformTransactionManager,从而能支持 ...

  9. 开涛spring3(3.3) - DI 之 3.3 更多DI的知识

    3.3.1  延迟初始化Bean 延迟初始化也叫做惰性初始化,指不提前初始化Bean,而是只有在真正使用时才创建及初始化Bean. 配置方式很简单只需在<bean>标签上指定 “lazy- ...

随机推荐

  1. Android查看stdout 和stderr

        在默认状态下,Android系统有stdout和stderr(System.out和System.err)输出到/dev/null,在运 行Dalvik VM的进程中,有一个系统可以备份日志文 ...

  2. 《Python基础教程》第1章读书笔记

    # -*- coding:utf-8 -*- x = "hello " y = "world" print x+y print "hello &quo ...

  3. 第一章:Druid简介

    第一章:Druid简介 声明 公司的项目中用到了Druid(不是阿里的连接池),由于网上没有中文的文档,所以只好阅读官方文档.本人第一次阅读英文的文档,非常吃力,借助翻译工具和自己的理解阅读了Duri ...

  4. java多线程基本概述(三)——同步块

    1.1.synchronized方法的弊端 package commonutils; public class CommonUtils { public static long beginTime1; ...

  5. angular 过滤排序

    <table class="table"> <thead> <tr> <th ng-click="changeOrder('id ...

  6. Elasticsearch实现类似 like '?%' 搜索

    在做搜索的时候,下拉联想词的搜索肯定是最常见的一个场景,用户在输入的时候,要自动补全词干,说得简单点,就是以...开头搜索,如果是数据库,一句SQL就很容易实现,但在elasticsearch如何实现 ...

  7. A GDI+ Based Character LCD Control

    This is a renew. A GDI+ Based Character LCD Control by Conmajia Character liquid crystal display (LC ...

  8. ActiveMQ学习心得:连接池的简单实现和模板模式的应用

    一.安装activemq 下载地址:https://archive.apache.org/dist/activemq/5.13.0/apache-activemq-5.13.0-bin.zip 下载完 ...

  9. List<Object>对象集合一些扩展方法

    // 商品集合信息            List<Product> list = new List<Product>()            {               ...

  10. CSS表单属性

    一般来说,表单在一个页面中是必不可少的,下面是我对表单的知识总结: 依次要说的是表单元素.表单属性.以及表单提交(js知识) 1,表单元素: <form action="提交的位置 / ...