使用路径通配符加载Resource,


http://blog.csdn.net/liufengyinglxj/article/details/8646417(这个比较好)

下面又是一个:

spring提供了强大的Ant模式通配符匹配,从同一个路径能匹配一批资源。

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

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

“*”:匹配零个或多个字符串,如“com/*/config.xml”将匹配“cn/feng/config.xml”,但不匹配匹配“com/config.xml”(因为这里匹配的是字符串,如果是目录的话则可以);而“com/config-*.xml”将匹配“com/config-dao.xml”;

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

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

Spring提供了ResourcePatternResolver接口来加载多个Resource.

[java] view plain copy

    package com.feng.spring.chapter2.helloworld; 
     
    import java.io.IOException; 
     
    import org.springframework.core.io.Resource; 
    import org.springframework.core.io.ResourceLoader; 
     
    public interface ResourcePatternResolver extends ResourceLoader{ 
        String CLASSPATH_ALL_URL_PREFIX = "classpath*:"; 
        Resource[] getResources(String locationPattern) throws IOException;//添加了此方法用来接收多个Resource 
    } 


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

[java] view plain copy

    package com.feng.spring.chapter2.helloworld; 
     
     
    import java.io.IOException; 
     
    import org.junit.Test; 
    import org.springframework.core.io.Resource; 
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 
     
     
    public class HelloTest { 
            @Test 
            public void testClasspathPrefix()throws IOException{ 
                ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); 
                //只加载一个绝对匹配Resource,且通过ResourceLoader.getResource进行加载 
                Resource resources=resolver.getResource("classpath:META-INF/INDEX.LIST"); 
                 Assert.assertEquals(1, resources.length); 
                //只加载一个匹配的Resource,且通过ResourceLoader.getResource进行加载 
                 resources = resolver.getResource("classpath:META-INF/*.LIST"); 
                 Assert.assertTrue(resources.length == 1);  
            } 
    } 


二、"classpath*":

用于加载类路径(包括jar包)中所有的匹配的资源。

[java] view plain copy

    package com.feng.spring.chapter2.helloworld; 
     
     
    import java.io.IOException; 
     
    import javax.annotation.Resource; 
     
    import junit.framework.Assert; 
     
    import org.junit.Test; 
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver; 
     
     
    public class HelloTest { 
            @Test 
            public void testClasspathAsteriskPrefix()throws IOException{ 
                ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); 
                 
                //将加载多个绝对匹配的所有Resource 
                //将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分 
                //然后进行遍历模式匹配 
                Resource[] resources = (Resource[]) resolver.getResources("classpath*:META-INF/INDEX.LIST");  
                Assert.assertTrue(resources.length > 1);   
                //将加载多个模式匹配的Resource 
                resources = (Resource[]) resolver.getResources("classpath*:META-INF/*.LIST"); 
                 Assert.assertTrue(resources.length > 1);    
            } 
    } 




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

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

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

ApplicationContext提供的getResource方法将获取资源委托给ResourcePatternResolver实现,默认使用PathMatingResourcePatternResolver.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package cn.javass.spring.chapter4; import java.io.File; import java.io.IOException; import junit.framework.Assert; import org.jboss.vfs.VFS; import org.jboss.vfs.VirtualFile; import org.jboss.vfs.spi.RealFileSystem; import org.junit.Test; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; @SuppressWarnings("all") public class ResourcePatternTest { @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); //只一个绝对匹配Resource,且通过ResourceLoader.getResource进行 resources = resolver.getResources("classpath:META-INF/MANIFEST.MF"); Assert.assertEquals(1, resources.length); } @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); } @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); } @Test public void testFilekPrefix() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resolver.getResources("file:D:/*.txt"); Assert.assertTrue(resources.length > 0); } @Test public void testVfsPrefix() throws IOException { //1.创建一个虚拟的文件目录 VirtualFile home = VFS.getChild("/home"); //2.将虚拟目录映射到物理的目录 VFS.mount(home, new RealFileSystem(new File("d:"))); //3.通过虚拟目录获取文件资源 VirtualFile testFile = home.getChild("test.txt"); ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resolver.getResources("/home/test.txt"); Assert.assertTrue(resources.length > 0); System.out.println(resources[0].getClass()); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值