`
soltex
  • 浏览: 10206 次
  • 来自: ...
社区版块
存档分类
最新评论

Spring Resource通配符路径

阅读更多
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:

java代码:
查看复制到剪贴板打印
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*:”前缀;

java代码:
查看复制到剪贴板打印
@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<URL> getResources(String name)”方法来查找通配符之前的资源,然后通过模式匹配来获取匹配的资源。如“classpath:META-INF/*.LIST”将首先加载通配符之前的目录“META-INF”,然后再遍历路径进行子路径匹配从而获取匹配的资源。

java代码:
查看复制到剪贴板打印
@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包根路径。


java代码:
查看复制到剪贴板打印
@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个资源。

       因此在通过前缀“classpath*”加载通配符路径时,必须包含一个根目录才能保证加载的资源是所有的,而不是部分。


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

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

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

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

java代码:
查看复制到剪贴板打印
<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”是混合使用的;测试代码在“cn.javass.spring.chapter4.ResourceInjectTest.testResourceArrayInject”。
       Spring通过ResourceArrayPropertyEditor来进行类型转换的,而它又默认使用“PathMatchingResourcePatternResolver”来进行把路径解析为Resource对象。所有大家只要会使用“PathMatchingResourcePatternResolver”,其它一些实现都是委托给它的,比如AppliacationContext的“getResources”方法等。

4.4.3  AppliacationContext实现对各种Resource的支持
       一、ClassPathXmlApplicationContext:默认将通过classpath进行加载返回ClassPathResource,提供两类构造器方法:

java代码:
查看复制到剪贴板打印
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。

java代码:
查看复制到剪贴板打印
public class FileSystemXmlApplicationContext{ 
       public FileSystemXmlApplicationContext(String configLocation); 
       public FileSystemXmlApplicationContext(String... configLocations,……); 




java代码:
查看复制到剪贴板打印
//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()方法如果以“/”开头则表示获取绝对路径资源,而不带前导“/”将返回相对路径资源。如下:

java代码:
查看复制到剪贴板打印
//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(1-7).pdf

    Spring概述2.1 IoC基础2.2 IoC 容器基本原理2.3 IoC的配置使用——跟我学Spring33.1 DI的配置使用3.2 循环依赖3.3 更多DI的知识 3.4 Bean的作用域 4.1 基础知识4.2 内置Resource实现4.3 访问Resource4.4 Resource...

    Java使用路径通配符加载Resource与profiles配置使用详解

    Java使用路径通配符加载Resource与profiles配置使用详解 本文主要介绍了Java使用路径通配符加载Resource与profiles配置使用详解。通过示例代码,详细介绍了Java使用路径通配符加载Resource与profiles配置的使用方法...

    跟我学spring3(1-7)

    【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3 【第五章...

    spring的学习笔记

    - **4.4 Resource通配符路径**:支持使用通配符加载一组资源,方便进行批量操作。 ### 5. **Spring Expression Language (SpEL)** SpEL是Spring提供的强大表达式语言,用于在运行时查询和操作Bean的属性,以及执行...

    跟我学Spring

    - **4.4 Resource通配符路径**:探讨了如何使用通配符在Spring中加载资源文件。 8. **Spring表达式语言(SpEL)**: - **5.1 概述**:简述了SpEL的作用和重要性,它是Spring框架中用于在运行时表达和评估表达式的...

    跟我学spring3

    - **4.4 Resource通配符路径**:介绍了使用通配符进行资源查找的方法,增强了资源管理的灵活性。 5. **Spring Expression Language (SpEL)**: - **5.1 概述**:SpEL是Spring提供的强大表达式语言,用于在运行时...

    Spring3(1-7)

    - **4.4 Resource通配符路径**:展示了如何使用通配符来加载一组相关的资源。 通过这个教程,读者将能够熟练掌握Spring 3.0的核心特性和最佳实践,从而更好地在实际项目中运用Spring框架,提高开发效率和代码质量...

    跟开涛学Spring

    1.14 【第四章】 资源 之 4.4 Resource通配符路径 ——跟我学spring3 . . . . . . . . . . . . . . . . . . . . . . . .171 1.15 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3 . . . . ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    4.7.2. Application context构造器中资源路径的通配符 4.7.2.1. Ant风格的pattern 4.7.2.2. classpath*: 前缀 4.7.2.3. 其他关于通配符的说明 4.7.3. FileSystemResource 提示 5. 校验,数据绑定,BeanWrapper,与...

    跟我学spring3(1-7)1

    4. **4.4 Resource通配符路径**:解释了如何使用通配符来匹配和加载多个资源。 **第五章 Spring表达式语言(SpEL)** SpEL是Spring提供的强大表达式语言,用于在运行时查询和操作对象图: 1. **5.1 概述**:简述...

    spring3.x的读书笔记-2

    在Spring框架中,Resource接口是核心组件之一,用于表示应用程序中的资源,比如文件、网络连接、输入/输出流等。在上述代码示例中,我们可以看到两种Resource的实例化方式:FileSystemResource和ClassPathResource。...

    Spring 2.0 开发参考手册

    4.7.2. Application context构造器中资源路径的通配符 4.7.3. FileSystemResource 提示 5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到...

    Spring中文帮助文档

    4.7.2. Application context构造器中资源路径的通配符 4.7.3. FileSystemResource 说明 5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到...

    spring chm文档

    4.7.2. Application context构造器中资源路径的通配符 4.7.3. FileSystemResource 提示 5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到...

    Spring API

    4.7.2. Application context构造器中资源路径的通配符 4.7.3. FileSystemResource 说明 5. 校验,数据绑定,BeanWrapper,与属性编辑器 5.1. 简介 5.2. 使用Spring的Validator接口进行校验 5.3. 从错误代码到...

    maven相关资料

    或者用通配符: ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml"); 二、ClassPathXmlApplicationContext[只能读放在web-info/classes目录下的配置文件]和...

    mybatis 加载配置文件的方法(两种方式)

    -- 映射文件路径,可以集中写到一个地方,也可以与dao写到一个地方,支持多个路径,支持通配符--&gt; *.xml,classpath:com/sunny/shop/*/dao/*.xml"&gt; ``` 在上面的代码中,我们使用了 sqlSessionFactory 的 ...

    ajax跨域jar包.zip

    为了解决这一问题,开发者通常采用CORS(Cross-Origin Resource Sharing,跨源资源共享)机制。 CORS是一种允许浏览器在用户代理之间安全地进行跨域请求的W3C标准。它通过在服务器端添加特定的HTTP头部,来告诉...

Global site tag (gtag.js) - Google Analytics