http://blog.csdn.net/liufengyinglxj/article/details/8646417(这个比较好) 下面又是
一个: spring提供了强大的Ant模式
通配符匹配,从同
一个路径能匹配一批资源。 Ant路径
通配符
支持"?"、"*"、"**",注意
通配符匹配不
包括目录分隔符“/”。 “?”:匹配
一个字符,如"con
fig?.xml"可匹配"con
fig1.xml". “*”:匹配零个或多个字符串,如“com/*/con
fig.xml”将匹配“cn/feng/con
fig.xml”,但不匹配匹配“com/con
fig.xml”(因为这里匹配的是字符串,如果是目录的话则可以);而“com/con
fig-*.xml”将匹配“com/con
fig-dao.xml”; "**":匹配路径中的零个或多个目录。如“com/**/con
fig.xml”将匹配“com/con
fig.xml”,也匹配“com/feng/spring/con
fig.xml”;而“com/feng/con
fig-**.xml”将匹配“com/feng/con
fig-dao.xml”,即把“**”当做两个“*”处理。 Spring在加载类路径资源时除了提供前缀“cla
sspath:”的来
支持加载
一个Resource,还提供
一个前缀“cla
sspath*:”来
支持加载所有匹配的类路径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 CLA
sspATH_ALL_URL_PREFIX = "cla
sspath*:"; Resource[] getResources(String locationPattern) throws IOException;//
添加了此
方法用来接收多个Resource } 一、"cla
sspath":用于加载类路径(
包括jar包)中的
一个且仅
一个资源;对于多个匹配的也只返回
一个。如果需要多个匹配的则考虑"cla
sspath*."前缀。 [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 testCla
sspathPrefix()throws IOException{ ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); //只加载
一个绝对匹配Resource,且通过ResourceLoader.getResource进行加载 Resource resources=resolver.getResource("cla
sspath:
meta-inf/INDEX.LIST"); Assert.assertEquals(1,resources.length); //只加载
一个匹配的Resource,且通过ResourceLoader.getResource进行加载 resources = resolver.getResource("cla
sspath:
meta-inf/*.LIST"); Assert.assertTrue(resources.length == 1); } } 二、"cla
sspath*": 用于加载类路径(
包括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 testCla
sspathAsteriskPrefix()throws IOException{ ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver(); //将加载多个绝对匹配的所有Resource //将首先通过ClassLoader.getResource("
meta-inf")加载非模式路径部分 //然后进行遍历模式匹配 Resource[] resources = (Resource[]) resolver.getResources("cla
sspath*:
meta-inf/INDEX.LIST"); Assert.assertTrue(resources.length > 1); //将加载多个模式匹配的Resource resources = (Resource[]) resolver.getResources("cla
sspath*:
meta-inf/*.LIST"); Assert.assertTrue(resources.length > 1); } } 带
通配符的cla
sspath使用“ClassLoader”的“Enumeration<URL> getResources(String name)”
方法来查找
通配符之前的资源,然后通过模式匹配来
获取匹配的资源。如“cla
sspath:
meta-inf/*.LIST”将首先加载
通配符之前的目录“
meta-inf”,然后再遍历路径进行子路径匹配从而
获取匹配的资源。 三、"file":加载
一个或多个系统中的Resource。如:"file:D/*.txt"将返回D盘下的所有txt
文件。 四、无前缀:通过ResourceLoader实现加载
一个资源。 ApplicationContext提供的getResource
方法将
获取资源委托给ResourcePatternResolver实现,
默认使用PathMatingResourcePatternResolver.